Skip to content

Commit 132e292

Browse files
committed
Fatal calls exit, Panic calls panic
1 parent f22d3bc commit 132e292

File tree

6 files changed

+52
-27
lines changed

6 files changed

+52
-27
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# v0.5.0 (Jan 13, 2023)
2+
3+
- Fatal calls to os.Exit(1).
4+
- Panic calls to panic().
5+
16
# v0.4.1 (Jan 6, 2023)
27

38
- Change name of NewStreamEmitter to NewBufferEmitter.

config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ func init() {
4444
const (
4545
NOTLOG = 1000
4646
CRITICAL = 50
47-
FATAL = CRITICAL
4847
ERROR = 40
4948
WARNING = 30
5049
WARN = WARNING

event_logger.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020

2121
package xylog
2222

23-
import "sync"
23+
import (
24+
"os"
25+
"sync"
26+
)
2427

2528
var eventLoggerPool = sync.Pool{
2629
New: func() any {
@@ -81,14 +84,6 @@ func (e *EventLogger) Error() {
8184
}
8285
}
8386

84-
// Fatal calls Log with FATAL level.
85-
func (e *EventLogger) Fatal() {
86-
defer e.free()
87-
if e.lg.isEnabledFor(FATAL) {
88-
e.lg.log(FATAL, e.fields...)
89-
}
90-
}
91-
9287
// Critical calls Log with CRITICAL level.
9388
func (e *EventLogger) Critical() {
9489
defer e.free()
@@ -97,6 +92,18 @@ func (e *EventLogger) Critical() {
9792
}
9893
}
9994

95+
// Fatal calls Log with CRITICAL level, then followed by a call to os.Exit(1).
96+
func (e *EventLogger) Fatal() {
97+
e.Critical()
98+
os.Exit(1)
99+
}
100+
101+
// Panic calls Log with CRITICAL level, then followed by a call to panic().
102+
func (e *EventLogger) Panic() {
103+
e.Critical()
104+
panic(nil)
105+
}
106+
100107
// Log logs with a custom level.
101108
func (e *EventLogger) Log(level int) {
102109
defer e.free()

event_logger_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestEventLogger(t *testing.T) {
6060
Test(t)
6161

6262
w.Reset()
63-
logger.Event(msg).Fatal()
63+
xycond.ExpectPanic(nil, logger.Event(msg).Panic).Test(t)
6464
xycond.ExpectIn(fmt.Sprintf("event=\"%s\"", msg), w.Captured).
6565
Test(t)
6666

logger.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package xylog
2222

2323
import (
2424
"fmt"
25+
"os"
2526
"runtime/debug"
2627
"strings"
2728

@@ -243,20 +244,6 @@ func (lg *Logger) Errorf(s string, a ...any) {
243244
}
244245
}
245246

246-
// Fatal logs default formatting objects with FATAL level.
247-
func (lg *Logger) Fatal(s string) {
248-
if lg.isEnabledFor(FATAL) {
249-
lg.log(FATAL, makeField("messsage", s))
250-
}
251-
}
252-
253-
// Fatalf logs a formatting message with FATAL level.
254-
func (lg *Logger) Fatalf(s string, a ...any) {
255-
if lg.isEnabledFor(FATAL) {
256-
lg.log(FATAL, makeField("messsage", fmt.Sprintf(s, a...)))
257-
}
258-
}
259-
260247
// Critical logs default formatting objects with CRITICAL level.
261248
func (lg *Logger) Critical(s string) {
262249
if lg.isEnabledFor(CRITICAL) {
@@ -271,6 +258,34 @@ func (lg *Logger) Criticalf(s string, a ...any) {
271258
}
272259
}
273260

261+
// Fatal logs default formatting objects with CRITICAL level, then followed by a
262+
// call to os.Exit(1).
263+
func (lg *Logger) Fatal(s string) {
264+
lg.Critical(s)
265+
os.Exit(1)
266+
}
267+
268+
// Fatalf logs a formatting message with CRITICAL level, then followed by a call
269+
// to os.Exit(1).
270+
func (lg *Logger) Fatalf(s string, a ...any) {
271+
lg.Criticalf(s, a...)
272+
os.Exit(1)
273+
}
274+
275+
// Panic logs default formatting objects with CRITICAL level, then followed by a
276+
// call to panic().
277+
func (lg *Logger) Panic(s string) {
278+
lg.Critical(s)
279+
panic(s)
280+
}
281+
282+
// Panicf logs a formatting message with CRITICAL level, then followed by a call
283+
// to panic().
284+
func (lg *Logger) Panicf(s string, a ...any) {
285+
lg.Criticalf(s, a...)
286+
panic(fmt.Sprintf(s, a...))
287+
}
288+
274289
// Log logs default formatting objects with a custom level.
275290
func (lg *Logger) Log(level int, s string) {
276291
level = CheckLevel(level)

logger_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ func TestLoggerLogMethods(t *testing.T) {
4949
{logger.Warnf, logger.Warn},
5050
{logger.Warningf, logger.Warning},
5151
{logger.Errorf, logger.Error},
52-
{logger.Fatalf, logger.Fatal},
5352
{logger.Criticalf, logger.Critical},
5453
}
5554

@@ -181,7 +180,7 @@ func TestLoggerFindCaller(t *testing.T) {
181180

182181
logger.Error("foo")
183182

184-
xycond.ExpectIn("lineno=182", w.Captured).Test(t)
183+
xycond.ExpectIn("lineno=181", w.Captured).Test(t)
185184
xycond.ExpectIn(
186185
"module=github.com/xybor-x/xylog_test", w.Captured).Test(t)
187186
xycond.ExpectIn(

0 commit comments

Comments
 (0)