@@ -18,19 +18,51 @@ A simple but powerful golang log Toolkit
1818go get github.com/phachon/go-logger
1919go get ./...
2020```
21+
2122# Requirement
2223go 1.8
2324
2425# Support outputs
25- - console //write console
26- - file //write file
26+ - console // write console
27+ - file // write file
2728- api // http request url
2829- ...
2930
3031
31- # Use
32+ # Quick Used
3233
33- - ### example
34+ - sync
35+
36+ ```
37+ import (
38+ "github.com/phachon/go-logger"
39+ )
40+ func main() {
41+ logger := go_logger.NewLogger()
42+
43+ logger.Info("this is a info log!")
44+ logger.Errorf("this is a error %s log!", "format")
45+ }
46+ ```
47+
48+ - async
49+
50+ ```
51+ import (
52+ "github.com/phachon/go-logger"
53+ )
54+ func main() {
55+ logger := go_logger.NewLogger()
56+ logger.SetAsync()
57+
58+ logger.Info("this is a info log!")
59+ logger.Errorf("this is a error %s log!", "format")
60+
61+ logger.Flush()
62+ }
63+ ```
64+
65+ - Multiple output
3466
3567```
3668import (
@@ -39,106 +71,86 @@ import (
3971func main() {
4072 logger := go_logger.NewLogger()
4173
42- // The default has been added to the output of console, and the default does not display the color. If you need to modify it, delete the console first
4374 logger.Detach("console")
4475
45- // config console
76+ // console adapter config
4677 consoleConfig := &go_logger.ConsoleConfig{
47- Color: true, // text show color
48- JsonFormat: true, // json format
49- ShowFileLine: true, // when JsonFormat is false, console show file line, default false
78+ Color: true, // Does the text display the color
79+ JsonFormat: true, // Whether or not formatted into a JSON string
80+ Format: "" // JsonFormat is false, logger message output to console format string
5081 }
51- // attach console to outputs
82+ // add output to the console
5283 logger.Attach("console", go_logger.LOGGER_LEVEL_DEBUG, consoleConfig)
5384
54- // config file
55- fileConfig := &go_logger.FileConfig{
56- Filename : "./test.log", // The file name of the log output does not exist automatically.
57- // If you want to output different levels of logs to a file individually , configure the LevelFileName parameter .
85+ // file adapter config
86+ fileConfig := &go_logger.FileConfig {
87+ Filename : "./test.log", // The file name of the logger output, does not exist automatically
88+ // If you want to separate separate logs into files , configure LevelFileName parameters .
5889 LevelFileName : map[int]string {
5990 logger.LoggerLevel("error"): "./error.log", // The error level log is written to the error.log file.
60- logger.LoggerLevel("info"): "./info.log", // The info level log is written to the info.log file.
91+ logger.LoggerLevel("info"): "./info.log", // The info level log is written to the info.log file.
6192 logger.LoggerLevel("debug"): "./debug.log", // The debug level log is written to the debug.log file.
6293 },
63- MaxSize : 1024 * 1024, // max file size(kb)
64- MaxLine : 100000, // max file line
65- DateSlice : "d", // slice file by date, support "y", "m", "d", "h", default "" not slice
66- JsonFormat: true, // json format
94+ MaxSize : 1024 * 1024, // File maximum (KB), default 0 is not limited
95+ MaxLine : 100000, // The maximum number of lines in the file, the default 0 is not limited
96+ DateSlice : "d", // Cut the document by date, support "Y" (year), "m" (month), "d" (day), "H" (hour), default "no".
97+ JsonFormat: true, // Whether the file data is written to JSON formatting
98+ Format: "" // JsonFormat is false, logger message written to file format string
6799 }
100+ // add output to the file
68101 logger.Attach("file", go_logger.LOGGER_LEVEL_DEBUG, fileConfig)
69102
70- // Set to asynchronous, default is synchronous output
71- logger.SetAsync()
72103
73- logger.Emergency("this is a emergency log!")
74- logger.Alert("this is a alert log!")
75- logger.Critical("this is a critical log!")
76- logger.Error("this is a error log!")
77- logger.Warning("this is a warning log!")
78- logger.Notice("this is a notice log!")
79104 logger.Info("this is a info log!")
80- logger.Debug("this is a debug log!")
81-
82- logger.Emergencyf("this is a emergency %d log!", 10)
83- logger.Alertf("this is a alert %s log!", "format")
84- logger.Criticalf("this is a critical %s log!", "format")
85105 logger.Errorf("this is a error %s log!", "format")
86- logger.Warningf("this is a warning %s log!", "format")
87- logger.Noticef("this is a notice %s log!", "format")
88- logger.Infof("this is a info %s log!", "format")
89- logger.Debugf("this is a debug %s log!", "format")
90-
91- // If set to asynchronous, the flush method must finally be invoked to ensure that all the logs are out
92- logger.Flush()
93- }
94- ```
95- - ### console adapter
96- ```
97- // config console
98- consoleConfig := &go_logger.ConsoleConfig{
99- Color: true, // text show color
100- JsonFormat: true, // json format
101- ShowFileLine: true, // when JsonFormat is false, console show file line, default false
102106}
103- // attach
104- logger.Attach("console", go_logger.LOGGER_LEVEL_DEBUG, consoleConfig)
105107```
106- #### console color preview
108+
109+ ## Console text with color effect
107110![ image] ( https://github.com/phachon/go-logger/blob/master/_example/images/console.png )
108111
109- - ### file adapter
112+ ## Customize Format output
110113
114+ Logger Message
115+
116+ | Field | Alias | Type | Comment | Example |
117+ | Timestamp | timestamp | int64 | unix timestamp| 1521791201 |
118+ | TimestampFormat | timestamp_format| string | timestamp format | 2018-3-23 15:46:41|
119+ | Millisecond | millisecond | int64 | millisecond | |
120+ | MillisecondFormat | millisecond_format| string | millisecond_format | 2018-3-23 15:46:41.970 |
121+ | Level | level| int | logger level | 1 |
122+ | LevelString | level_string | string | logger level string | Error |
123+ | Body | body | string | logger message body | this is a info log |
124+ | File | file | string | Call the file of the logger | main.go |
125+ | Line | line | int | The number of specific lines to call logger | 64|
126+ | Function | function| string | The function name to call logger | main() |
127+
128+ If you want to customize the format of the log output ?
129+
130+ config format:
111131```
112- // config file
132+ consoleConfig := &go_logger.ConsoleConfig{
133+ Format: "%millisecond_format% [%level_string%] %body%"
134+ }
113135fileConfig := &go_logger.FileConfig{
114- Filename : "./test.log", // The file name of the log output does not exist automatically.
115- // If you want to output different levels of logs to a file individually, configure the LevelFileName parameter.
116- LevelFileName : map[int]string {
117- go_logger.LOGGER_LEVEL_ERROR: "./error.log", // The error level log is written to the error.log file.
118- go_logger.LOGGER_LEVEL_INFO: "./info.log", // The info level log is written to the info.log file.
119- go_logger.LOGGER_LEVEL_DEBUG: "./debug.log", // The debug level log is written to the debug.log file.
120- },
121- MaxSize : 1024 * 1024, // max file size(kb)
122- MaxLine : 100000, // max file line
123- DateSlice : "d", // slice file by date, support "y", "m", "d", "h", default "" not slice
124- JsonFormat: true, // json format
136+ Format: "%millisecond_format% [%level_string%] %body%"
125137}
126- logger.Attach("file", go_logger.LOGGER_LEVEL_DEBUG, fileConfig)
127138```
128-
129- - ### api adapter
130-
139+ output:
131140```
132- apiConfig := &go_logger.ApiConfig{
133- Url: "http://127.0.0.1:8081/index.php", //request url address, not empty
134- Method: "GET", //request method GET or POST
135- Headers: map[string]string{}, //request headers, default empty
136- IsVerify: false, //response is verify code, default false
137- VerifyCode: 0, //verify code value, if isVerify is true, verifyCode is not be 0
138- }
139- logger.Attach("api", go_logger.LOGGER_LEVEL_DEBUG, apiConfig)
141+ 2018-03-23 14:55:07.003 [Critical] this is a critical log!
140142```
141143
144+ You can customize the format, Only needs to be satisfied Format: "%Logger Message Alias%"
145+
146+ ## More adapter examples
147+ - [ console] ( ./_example/console.go )
148+ - [ file] ( ./_example/file.go )
149+ - [ api] ( ./_example/api.go )
150+
151+
152+ ## Benchmark
153+
142154## Reference
143155beego/logs : github.com/astaxie/beego/logs
144156
0 commit comments