You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
panic("unable to create new logger: " + err.Error())
56
+
}
57
+
}
73
58
```
74
59
75
-
In production, you may want to make log less verbose and asynchronous:
60
+
- The `0` is an integer type so it is used as underlying buffer size. In this case, `0` creates synchronized logger (call hangs until write is finished).
61
+
- Any non-integer type is used as the config object, in this case `ConsoleConfig` is the respective config object for the console logger.
62
+
- The `LevelTrace` used here is the lowest logging level, meaning prints every log to the console. All levels from lowest to highest are: `LevelTrace`, `LevelInfo`, `LevelWarn`, `LevelError`, `LevelFatal`, each of has at least one repective function, e.g. `log.Trace`, `log.Info`, `log.Warn`, `log.Error` and `log.Fatal`.
63
+
64
+
In production, you may want to make log less verbose and be asynchronous:
76
65
77
66
```go
78
-
...
79
-
// The buffer size mainly depends on how many logs will be produced at the same time,
80
-
// 100 is a good default.
67
+
funcinit() {
68
+
// The buffer size mainly depends on how many logs will be produced at the same time, 100 is a good default.
// Logs under Info level (in this case Trace) will be discarded.
83
70
Level: log.LevelInfo,
84
71
})
85
-
...
72
+
if err != nil {
73
+
panic("unable to create new logger: " + err.Error())
74
+
}
75
+
}
86
76
```
87
77
88
-
Console logger comes with color output, but for non-colorable destination, the color output will be disabled automatically.
78
+
- When you set level to be `LevelInfo`, calls to the `log.Trace` will be simply noop.
79
+
- The console logger comes with color output, but for non-colorable destination, the color output will be disabled automatically.
80
+
81
+
Other builtin loggers are file (`log.ModeFile`), Slack (`log.ModeSlack`) and Discord (`log.Discord`), see later in the documentation for usage details.
89
82
90
-
### Error Location
83
+
### Caller Location
91
84
92
-
When using `log.Error` and `log.Fatal` functions, the caller location is printed along with the message.
85
+
When using `log.Error` and `log.Fatal` functions, the caller location is written along with logs.
If you want to have different skip depth than the default, you can use `log.ErrorDepth` or `log.FatalDepth`.
96
+
- Calling `log.Fatal` will exit the program.
97
+
- If you want to have different skip depth than the default, use `log.ErrorDepth` or `log.FatalDepth`.
106
98
107
99
### Clean Exit
108
100
109
-
You should always call `log.Stop()` to wait until all messages are processed before program exits.
101
+
You should always call `log.Stop()` to wait until all logs are processed before program exits.
102
+
103
+
## Builtin Loggers
110
104
111
-
## File
105
+
###File Logger
112
106
113
-
File logger is more complex than console, and it has ability to rotate:
107
+
File logger is the single most powerful builtin logger, it has the ability to rotate based on file size, line, and date:
114
108
115
109
```go
116
-
...
110
+
funcinit() {
117
111
err:= log.New(log.ModeFile, 100, log.FileConfig{
118
-
Level: log.LevelInfo,
112
+
Level: log.LevelInfo,
119
113
Filename: "clog.log",
120
114
FileRotationConfig: log.FileRotationConfig {
121
115
Rotate: true,
122
116
Daily: true,
123
117
},
124
118
})
125
-
...
119
+
if err != nil {
120
+
panic("unable to create new logger: " + err.Error())
121
+
}
122
+
}
123
+
```
124
+
125
+
In case you have some other packages that write to a file, and you want to take advatange of this file rotation feature. You can do so by using the `log.NewFileWriter` function. It acts like a standard `io.Writer`.
0 commit comments