package main
import "github.com/mathbalduino/go-log"
func main() { // Outputs to stdout yourLogger := logger.NewDefault()
yourLogger.Info("New INFO log message") // [ INFO ] New INFO log message yourLogger.Error("New ERROR log message") // [ ERROR ] New ERROR log message // ...}
Easy to Use
Just create a new default Logger instance and start to focus on your application development
package main
import ( "github.com/mathbalduino/go-log" "os")
func main() { // Outputs to stdout userLogger := logger.New(logger.DefaultConfig()). Fields(logger.LogFields{"module": "user"}). Outputs(logger.OutputJsonToWriter(os.Stdout, nil))
userLogger.Info("New log") // { "lvl": 4, "module": "user", "msg": "New log" }}
Base Log Fields
You can define how many Base fields as you want. These fields are constant values and will be used to compose every log created by the Logger instance
package main
import ( "github.com/mathbalduino/go-log" "os" "time")
func main() { // Outputs to stdout userLogger := logger.New(logger.DefaultConfig()). Fields(logger.LogFields{"module": "user"}). PreHooks(logger.Hooks{ "timestamp": func(logger.Log) interface{} { return time.Now().UnixNano() }, }). Outputs(logger.OutputJsonToWriter(os.Stdout, nil))
userLogger.Info("New log", logger.LogFields{"id": 98}) // { "id": 98, "lvl": 4, "module": "user", // "msg": "New log", "timestamp": 1633182921043120309 }}
Dynamic Log Fields
Some fields my need to be calculated every time a new log is created. To solve this problem, you can use PreHooks, AdHoc fields and PostHooks
package main
import ( "github.com/mathbalduino/go-log" "fmt")
// Parse log fields to JSON and send them to the cloudfunc OutputToCloud(_ uint64, _ string, fields logger.LogFields) { parseToJsonAndSendToCloud(fields)}
func OutputToStdout(lvl uint64, msg string, _ logger.LogFields) { fmt.Printf("LogLevel: %d | LogMsg: %s\n", lvl, msg)}
func main() { yourLogger := logger.New(logger.DefaultConfig()). Outputs(OutputToStdout, OutputToCloud) yourLogger.Info("New log") // stdout: "LogLevel: 4 | LogMsg: New log\n" // cloud: { "lvl": 4, "msg": "New log" }}
Output configuration
Every log has a destiny, and you can set as many Outputs as you want. If you don't want to implement your own outputs, just use the builtins