// Sets up Loggers func Setup() { maxLevel := func(verbosity int) int { max := len(levels) if verbosity > max { return max } return verbosity } Log = factorlog.New( os.Stdout, factorlog.NewStdFormatter(stdLogFormat)) LogDebug = factorlog.New( os.Stdout, factorlog.NewStdFormatter(debugLogFormat)) LogConsole = factorlog.New( os.Stdout, factorlog.NewStdFormatter(consoleLogFormat)) Log.SetMinMaxSeverity(levels[maxLevel(verbosity)], factorlog.ERROR) // Do not set logdebug and logconsole // LogDebug.SetMinMaxSeverity(levels[maxLevel(verbosity)], factorlog.ERROR) // LogConsole.SetMinMaxSeverity(levels[maxLevel(verbosity)], factorlog.ERROR) if verbosity >= maxVerbosity { File = factorlog.New( debugFile, factorlog.NewStdFormatter(fileLogFormat)) } }
func SetGlobalLogger(logPath string) *log.FactorLog { sfmt := `%{Color "red:white" "CRITICAL"}%{Color "red" "ERROR"}%{Color "yellow" "WARN"}%{Color "green" "INFO"}%{Color "cyan" "DEBUG"}%{Color "blue" "TRACE"}[%{Date} %{Time}] [%{SEVERITY}:%{ShortFile}:%{Line}] %{Message}%{Color "reset"}` logger := log.New(os.Stdout, log.NewStdFormatter(sfmt)) if len(logPath) > 0 { logf, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0640) if err != nil { return logger } logger = log.New(logf, log.NewStdFormatter(sfmt)) } logger.SetSeverities(log.INFO | log.WARN | log.ERROR | log.FATAL | log.CRITICAL) return logger }
//InitLogger creates a factorlog with the given min loglevel func InitLogger(minSeverity string) { mutex.Lock() logFormatter := factorlog.NewStdFormatter(logColors + logFormat) targetWriter := os.Stdout singleLogger = factorlog.New(targetWriter, logFormatter) singleLogger.SetMinMaxSeverity(factorlog.StringToSeverity(minSeverity), factorlog.StringToSeverity("PANIC")) mutex.Unlock() }
func main() { frmt := `%{Color "red" "ERROR"}%{Color "yellow" "WARN"}%{Color "green" "INFO"}%{Color "cyan" "DEBUG"}%{Color "blue" "TRACE"}[%{Date} %{Time}] [%{SEVERITY}:%{File}:%{Line}] %{Message}%{Color "reset"}` log := factorlog.New(os.Stdout, factorlog.NewStdFormatter(frmt)) log.Error("Severity: Error occurred") log.Warn("Severity: Warning!!!") log.Info("Severity: I have some info for you") log.Debug("Severity: Debug what?") log.Trace("Severity: Tracing your IP... muauahaha") }
func main() { log := factorlog.New(os.Stdout, gelf.NewGELFFormatter()) log.Print("GELF formatter") server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Print("GELF: client connected", gelf.Extra{"_hello": "there"}, r) })) http.Get(server.URL) }
//InitLogger Constructor. func InitLogger(logFile, minSeverity string) { var logFormatter factorlog.Formatter var targetWriter io.Writer var err error if logFile == "" { logFormatter = factorlog.NewStdFormatter(logColors + logFormat) targetWriter = os.Stdout } else { logFormatter = factorlog.NewStdFormatter(logFormat) targetWriter, err = os.OpenFile(logFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644) } if err != nil { panic(err) } singleLogger = factorlog.New(targetWriter, logFormatter) singleLogger.SetMinMaxSeverity(factorlog.StringToSeverity(minSeverity), factorlog.StringToSeverity("PANIC")) }
//Logger Constructor. func InitLogger(logFile, minSeverity string) { var logFormatter factorlog.Formatter var targetWriter io.Writer var err error if logFile == "" { logFormatter = factorlog.NewStdFormatter(logColors + logFormat) targetWriter = os.Stdout } else { logFormatter = factorlog.NewStdFormatter(logFormat) if _, err := os.Stat(logFile); err == nil { targetWriter, err = os.Create(logFile) } else { targetWriter, err = os.Open(logFile) } } if err != nil { panic(err) } singleLogger = factorlog.New(targetWriter, logFormatter) singleLogger.SetMinMaxSeverity(factorlog.StringToSeverity(minSeverity), factorlog.StringToSeverity("PANIC")) }
// We need initialize logging before all other modules. // It don't matter in regular program run but critical for unit tests. func init() { var ( debug = flag.Bool("debug", false, "debug output") verb = flag.Bool("verb", false, "verbose output") listen = flag.String("listen", "http://127.1:48084/ws", "listen HTTP requests at addr:port/path") logFmt string err error ) flag.Parse() listenURL, err = url.Parse(*listen) if *debug { // brief format for debug logFmt = `%{Color "red" "ERROR"}%{Color "red" "FATAL"}%{Color "yellow" "WARN"}%{Color "green" "INFO"}%{Color "cyan" "DEBUG"}%{Color "blue" "TRACE"}[%{Date} %{Time}] [%{SEVERITY}:%{File}:%{Line}] %{Message}%{Color "reset"}` } else { // short format for production logFmt = `%{Color "red" "ERROR"}%{Color "red" "FATAL"}%{Color "yellow" "WARN"}%{Color "green" "INFO"}%{Color "cyan" "DEBUG"}%{Color "blue" "TRACE"}[%{Date} %{Time}] [%{SEVERITY}] %{Message}%{Color "reset"}` } log = factorlog.New(os.Stderr, factorlog.NewStdFormatter(logFmt)) switch { case *debug: log.SetMinMaxSeverity(factorlog.TRACE, factorlog.PANIC) case *verb: log.SetMinMaxSeverity(factorlog.INFO, factorlog.PANIC) default: log.SetMinMaxSeverity(factorlog.WARN, factorlog.PANIC) } if err != nil { log.Criticalf("bad url %s", *listen) os.Exit(1) } if listenURL.Path == "" { listenURL.Path = "/" } }
func main() { log := factorlog.New(os.Stdout, factorlog.NewStdFormatter(`%{Color "magenta"}[%{Date} %{Time}] %{Color "cyan"}[%{SEVERITY}:%{File}:%{Line}] %{Color "yellow"}%{Message}%{Color "reset"}`)) log.Println("Color") }
func main() { log := factorlog.New(os.Stdout, factorlog.NewStdFormatter("%{Date} %{Time} %{File}:%{Line} %{Message}")) log.Println("Basic formatter") }
func init() { frmt := `%{Color "red" "ERROR"}%{Color "yellow" "WARN"}%{Color "green" "INFO"}%{Color "cyan" "DEBUG"}%{Color "blue" "TRACE"}[%{Date} %{Time}] [%{SEVERITY}:%{File}:%{Line}] %{Message}%{Color "reset"}` LOG = factorlog.New(os.Stdout, factorlog.NewStdFormatter(frmt)) LOG.SetMinMaxSeverity(factorlog.PANIC, factorlog.TRACE) }
func main() { log := log.New(os.Stdout, NewCustomFormatter()) log.Println("Custom formatter") }
package main import ( "fmt" "github.com/kdar/factorlog" "io/ioutil" "net/http" "os" "strings" ) var ip = "http://127.0.0.1:8080" var file, _ = os.OpenFile("logfile.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) var logg = factorlog.New(os.Stdout, factorlog.NewStdFormatter("%{Message}")) var loggFile = factorlog.New(file, factorlog.NewStdFormatter("%{Message}")) func Phantom(link string) { buf := strings.NewReader(link) resp, err := http.Post(ip, "text/plain", buf) if err != nil { fmt.Println(err) return } defer resp.Body.Close() bodyBytes, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println(err) return } bodyString := string(bodyBytes)
func main() { log := factorlog.New(os.Stdout, glog.NewGlogFormatter()) log.Print("Glog formatter") }
func Logger() { file, _ := os.OpenFile("logfile.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) log := factorlog.New(file, factorlog.NewStdFormatter("%{Date} %{Time}\tLOG\t%{Message}")) log.Println("Innerhalb Basic formatter") }
func init() { Log = new(log.NullLogger) if dbg := os.Getenv("XP_DBG"); dbg != "" { Log = log.New(os.Stdout, log.NewStdFormatter("%{File}:%{Line} >> %{Message}")) } }
//InitTestLogger creates logger for testing func InitTestLogger() { singleLogger = factorlog.New(os.Stderr, factorlog.NewStdFormatter("")) }