func init() { var handler log.Handler if term.IsTty(os.Stdout.Fd()) { handler = log.StreamHandler(os.Stdout, log.TerminalFormat()) } else { handler = log.StreamHandler(os.Stdout, log.JsonFormat()) } log.Root().SetHandler(handler) ncr.Log.SetHandler(handler) }
func MyFileHandler(path string, fmtr llog.Format) (llog.Handler, *os.File, error) { f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) if err != nil { return nil, f, err } return closingHandler{f, llog.StreamHandler(f, fmtr)}, f, nil }
func (w *Logged) HandlerFor(target, level, format string) (log.Handler, error) { lvl, err := log.LvlFromString(level) if err != nil { return nil, fmt.Errorf("Invalid log level: %v", err) } var logformat log.Format switch format { case "json": logformat = log.JsonFormat() case "logfmt": logformat = log.LogfmtFormat() case "terminal", "term": switch { case target == "stdout" && term.IsTty(os.Stdout.Fd()): logformat = log.TerminalFormat() case target == "stderr" && term.IsTty(os.Stderr.Fd()): logformat = log.TerminalFormat() default: logformat = log.LogfmtFormat() } default: return nil, fmt.Errorf("Invalid log format: %v", format) } var handler log.Handler switch target { case "stdout": handler = log.StreamHandler(colorable.NewColorableStdout(), logformat) case "stderr": handler = log.StreamHandler(colorable.NewColorableStderr(), logformat) case "false": handler = log.DiscardHandler() default: handler, err = log.FileHandler(target, logformat) if err != nil { return nil, fmt.Errorf("Failed to open log file '%s': %v", target, err) } } return log.LvlFilterHandler(lvl, handler), nil }
func InitLogger(slvl string) { if slvl == "" { slvl = "debug" } lvl, err := log.LvlFromString(slvl) if err != nil { panic(err) } Log.SetHandler(log.LvlFilterHandler(lvl, log.StreamHandler(os.Stdout, log.TerminalFormat()))) return }
// InitLog ... func (logM *LogManager) InitLog() { var svrlog = log.New(logM.LogContext, logM.LogContext) svrlog.SetHandler(log.MultiHandler(log.StreamHandler(os.Stderr, log.LogfmtFormat()), log.LvlFilterHandler(log.LvlError, log.Must.FileHandler(logM.LogDir+logM.ErrorFile, log.JsonFormat())), log.LvlFilterHandler(log.LvlInfo, log.Must.FileHandler(logM.LogDir+logM.InfoFile, log.JsonFormat())), log.LvlFilterHandler(log.LvlCrit, log.Must.FileHandler(logM.LogDir+logM.CritFile, log.JsonFormat())), log.LvlFilterHandler(log.LvlDebug, log.Must.FileHandler(logM.LogDir+logM.DebugFile, log.JsonFormat())))) logM.Logger = svrlog }
func New() *Engine { // package instance of the helios type engine := &Engine{ HTTPEngine: gin.Default(), Socket: initSocket(), Logger: log.New(), } fileHandler, _ := log.FileHandler("./log.debug", log.LogfmtFormat()) engine.SetHandler(log.MultiHandler(log.LvlFilterHandler(log.LvlWarn, fileHandler), log.StreamHandler(os.Stdout, log.TerminalFormat()))) return engine }
func BenchmarkLog15JSONPositive(b *testing.B) { stream := &blackholeStream{} logger := log.New() logger.SetHandler(log.StreamHandler(stream, log.JsonFormat())) b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { logger.Info("The quick brown fox jumps over the lazy dog", "rate", 15, "low", 16, "high", 123.2) } }) if stream.WriteCount() != uint64(b.N) { b.Fatalf("Log write count") } }
func ConfigureRootLogger() { sh := log.StreamHandler(colorable.NewColorableStdout(), log.TerminalFormat()) logLevel := log.LvlInfo if showDebug == true { logLevel = log.LvlDebug } else { _, found := os.LookupEnv("DEBUG") if found == true { logLevel = log.LvlDebug } } fh := log.LvlFilterHandler(logLevel, sh) cfh := log.CallerFileHandler(fh) log.Root().SetHandler(cfh) }
func setSettings(level int, filename string) { logFilename = filename debug = level filehandler := log.Must.FileHandler(filename, log.LogfmtFormat()) handler := log.MultiHandler( filehandler, log.StreamHandler(os.Stderr, log.TerminalFormat())) if debug > 1 { handler = log.CallerStackHandler("%+v", handler) } else { handler = log.CallerFileHandler(handler) } if debug < 1 { handler = log.LvlFilterHandler(log.LvlInfo, handler) } log.Root().SetHandler(handler) log.Info("Logging started", "level", debug, "log_file", logFilename) }
func BenchmarkLog15TextNegative(b *testing.B) { stream := &blackholeStream{} logger := log.New() logger.SetHandler(log.LvlFilterHandler( log.LvlError, log.StreamHandler(stream, log.LogfmtFormat())), ) b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { logger.Info("The quick brown fox jumps over the lazy dog") } }) if stream.WriteCount() != uint64(0) { b.Fatalf("Log write count") } }
func New() *Engine { // package instance of the helios type engine := &Engine{ HTTPEngine: gin.Default(), Logger: log.New(), } fileHandler, _ := log.FileHandler("./error.log", log.LogfmtFormat()) engine.SetHandler(log.MultiHandler(log.LvlFilterHandler(log.LvlWarn, fileHandler), log.StreamHandler(os.Stdout, log.TerminalFormat()))) socket, err := initSocket() if err != nil { engine.Error("Failed to initialize socket server") } // Handle error cases socket.On("error", func(so socketio.Socket, err error) { engine.Error("Error on socket.io server", "error", err.Error()) }) engine.Socket = socket return engine }
func level(max lg.Lvl) lg.Logger { l := lg.New() handler := lg.StreamHandler(os.Stdout, lg.LogfmtFormat()) l.SetHandler(lg.LvlFilterHandler(max, handler)) return l }