-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathconsole.go
56 lines (43 loc) · 1.5 KB
/
console.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package handler
import (
"os"
"github.com/gookit/color"
"github.com/gookit/slog"
)
/********************************************************************************
* console log handler
********************************************************************************/
// ConsoleHandler definition
type ConsoleHandler = IOWriterHandler
// NewConsoleWithLF create new ConsoleHandler and with custom slog.LevelFormattable
func NewConsoleWithLF(lf slog.LevelFormattable) *ConsoleHandler {
h := NewIOWriterWithLF(os.Stdout, lf)
// default use text formatter
f := slog.NewTextFormatter()
// default enable color on console
f.WithEnableColor(color.SupportColor())
h.SetFormatter(f)
return h
}
//
// ------------- Use max log level -------------
//
// ConsoleWithMaxLevel create new ConsoleHandler and with max log level
func ConsoleWithMaxLevel(level slog.Level) *ConsoleHandler {
return NewConsoleWithLF(slog.NewLvFormatter(level))
}
//
// ------------- Use multi log levels -------------
//
// NewConsole create new ConsoleHandler, alias of NewConsoleHandler
func NewConsole(levels []slog.Level) *ConsoleHandler {
return NewConsoleHandler(levels)
}
// ConsoleWithLevels create new ConsoleHandler and with limited log levels
func ConsoleWithLevels(levels []slog.Level) *ConsoleHandler {
return NewConsoleHandler(levels)
}
// NewConsoleHandler create new ConsoleHandler with limited log levels
func NewConsoleHandler(levels []slog.Level) *ConsoleHandler {
return NewConsoleWithLF(slog.NewLvsFormatter(levels))
}