func setUpLogging(logLevel logging.Level) { backend := logging.NewLogBackend(os.Stderr, "", 0) backendFormatted := logging.NewBackendFormatter(backend, logFormat) backendLeveled := logging.AddModuleLevel(backendFormatted) backendLeveled.SetLevel(logLevel, "") logging.SetBackend(backendLeveled) }
func init() { Logger = gologging.MustGetLogger("netspel") format := gologging.MustStringFormatter("%{time:2006-01-02T15:04:05.000000Z} %{level} %{message}") backend := gologging.NewLogBackend(os.Stdout, "", 0) backendFormatter := gologging.NewBackendFormatter(backend, format) LogLevel = gologging.AddModuleLevel(backendFormatter) gologging.SetBackend(LogLevel) }
func CreateLogger() *go_logging.Logger { var logger = go_logging.MustGetLogger("vamp-gateway-agent") var backend = go_logging.NewLogBackend(io.Writer(os.Stdout), "", 0) backendFormatter := go_logging.NewBackendFormatter(backend, go_logging.MustStringFormatter( "%{color}%{time:15:04:05.000} %{shortpkg:.4s} %{level:.4s} ==> %{message} %{color:reset}", )) go_logging.SetBackend(backendFormatter) return logger }
func init() { syncRoot = &sync.RWMutex{} format := gologging.MustStringFormatter("%{color}%{time:15:04:05:000} %{shortfunc} > %{level:.4s} %{id:03x}%{color:reset} %{message}") backend := gologging.NewLogBackend(os.Stdout, "", 0) backendFormatter := gologging.NewBackendFormatter(backend, format) backendLeveled = gologging.AddModuleLevel(backendFormatter) backendLeveled.SetLevel(gologging.INFO, "") gologging.SetBackend(backendLeveled) }
func setUpLogging() { backend := logging.NewLogBackend(os.Stderr, "", 0) format := logging.MustStringFormatter( "%{color}%{time:15:04:05.000}: %{module} %{level} %{color:reset} %{message}") backendFormatter := logging.NewBackendFormatter(backend, format) leveledBackendFormatter := logging.AddModuleLevel(backendFormatter) leveledBackendFormatter.SetLevel(logging.CRITICAL, "") leveledBackendFormatter.SetLevel(logging.DEBUG, "extest") logging.SetBackend(leveledBackendFormatter) }
// SetUpLogging sets up logging to output for the test framework func SetUpLogging(output io.Writer) { backend := logging.NewLogBackend(output, "", 0) format := logging.MustStringFormatter( "%{color}%{time:15:04:05.000}: %{module} %{level} %{color:reset} %{message}") backendFormatter := logging.NewBackendFormatter(backend, format) leveledBackendFormatter := logging.AddModuleLevel(backendFormatter) leveledBackendFormatter.SetLevel(logging.INFO, "") leveledBackendFormatter.SetLevel(logging.DEBUG, "extest") logging.SetBackend(leveledBackendFormatter) }
func SetSysLogger(syslog string) { syslogBackend, err := gologging.NewSyslogBackend(syslog) if err != nil { panic(err) } backendFormatter := gologging.NewBackendFormatter(syslogBackend, format) syslogLeveledBackend := gologging.AddModuleLevel(backendFormatter) backends = append(backends, syslogLeveledBackend) gologging.SetBackend(backends...) }
func newLog() (*logging.Logger, error) { fdFmt := logging.MustStringFormatter( `%{level:.4s}[%{id:03x}]%{time:2006-01-02 15:04:05.000} ▶ %{message}`, ) scrFmt := logging.MustStringFormatter( `%{color:bold}%{level:.4s}%{color:reset}[%{id:03x}]%{time:15:04:05.000} ▶ %{message}`, ) fd, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) fdlog := logging.NewLogBackend(fd, "", 0) fdFmttr := logging.NewBackendFormatter(fdlog, fdFmt) scr := logging.NewLogBackend(os.Stderr, "", 0) scrFmttr := logging.NewBackendFormatter(scr, scrFmt) logging.SetBackend(fdFmttr, scrFmttr) return logging.MustGetLogger(basename(os.Args[0])), err }
func (lc *LoggerConfig) newGoLogger() *gol.Logger { // Leveled formatted file backend. backend := gol.AddModuleLevel( gol.NewBackendFormatter( gol.NewLogBackend(lc.Out, "", 0), gol.MustStringFormatter(lc.Format))) backend.SetLevel(lc.Level, "") logger := gol.MustGetLogger("") logger.SetBackend(backend) return logger }
func TestParmsLog(t *testing.T) { Convey("Testing log()", t, func() { tests := []struct { dbug bool name string str string verb bool }{ {name: "Info", str: "This is a log.Info test", dbug: false, verb: true}, {name: "Debug", str: "This is a log.Debug test", dbug: true, verb: false}, {name: "Debug & Info", str: "This is both a log.Debug and log.Info test ", dbug: true, verb: true}, {name: "Both Debug or Info", str: "This is both a log.Debug and log.Info test and there should be output", dbug: true, verb: true}, {name: "Neither Debug or Info", str: "This is both a log.Debug and log.Info test and there shouldn't be any output", dbug: false, verb: false}, } var ( scrFmt = logging.MustStringFormatter(`%{level:.4s}[%{id:03x}] ▶ %{message}`) act = &bytes.Buffer{} p = &Parms{Logger: logging.MustGetLogger("TestParmsLog"), Verb: true} scr = logging.NewLogBackend(act, "", 0) scrFmttr = logging.NewBackendFormatter(scr, scrFmt) ) logging.SetBackend(scrFmttr) for i, tt := range tests { Convey("Testing "+tt.name, func() { p.Dbug = tt.dbug p.Verb = tt.verb switch { case tt.dbug: p.debug(tt.str) So(act.String(), ShouldEqual, fmt.Sprintf("DEBU[00%d] ▶ %v\n", i+1, tt.str)) case tt.verb: p.log(tt.str) So(act.String(), ShouldEqual, fmt.Sprintf("INFO[00%d] ▶ %v\n", i+1, tt.str)) case tt.dbug && tt.verb: exp := fmt.Sprintf("DEBU[00%d] ▶ %v\n", i+1, tt.str) exp += fmt.Sprintf("INFO[00%d] ▶ %v\n", i+1, tt.str) p.debug(tt.str) p.log(tt.str) So(act.String(), ShouldEqual, exp) default: p.debug(tt.str) p.log(tt.str) So(act.String(), ShouldEqual, "") } act.Reset() }) } }) }
func configureLogging(verbose bool) { backend := log.NewLogBackend(os.Stdout, "", 0) f := "%{time:15:04:05.000} %{module} ▶ %{level:.5s} %{id:03x} %{message}" // f = "%{time:15:04:05.000} %{module} ▶ %{level:.5s} %{id:03x} %{message}" format := log.MustStringFormatter(f) formatter := log.NewBackendFormatter(backend, format) log.SetBackend(formatter) log.SetLevel(log.ERROR, "lidc") if verbose { log.SetLevel(log.DEBUG, "lidc") } }
func setLogger() *logging.Logger { var log = logging.MustGetLogger("beano") var format = logging.MustStringFormatter( "%{color}%{time:15:04:05.000000} %{level:.5s} %{id:04d}%{color:reset} %{message}", ) var logBackend = logging.NewLogBackend(os.Stdout, "", 0) // bel := logging.AddModuleLevel(logBackend) // bel.SetLevel(logging.ERROR, "") var bf = logging.NewBackendFormatter(logBackend, format) logging.SetBackend(bf) return log }
func init() { Log = Logger{ log: gologging.MustGetLogger("default"), } format := gologging.MustStringFormatter(`%{shortfile} %{color} %{time:2006-01-02T15:04:05.000000Z} %{level:.4s} %{color:reset} %{message}`) backend := gologging.NewLogBackend(os.Stdout, "", 0) backendFormatter := gologging.NewBackendFormatter(backend, format) leveledBackend = gologging.AddModuleLevel(backendFormatter) gologging.SetBackend(leveledBackend) SetLogLevel(SILENT) }
func init() { Log = Logger{ log: gologging.MustGetLogger(defaultModule), } Log.log.ExtraCalldepth = 1 format = gologging.MustStringFormatter("%{time:2006-01-02T15:04:05.000000Z} %{shortfunc:.6s} %{level}: %{message}") backend := gologging.NewLogBackend(os.Stdout, "", 0) backendFormatter := gologging.NewBackendFormatter(backend, format) leveledBackend := gologging.AddModuleLevel(backendFormatter) backends = append(backends, leveledBackend) gologging.SetBackend(backends...) SetLevel(SILENT) }
// GetLog gets log handler func GetLog(level logging.Level) (l *logging.Logger) { l = logging.MustGetLogger("ghostbox") var customLogFormat = logging.MustStringFormatter( "%{color}%{time:2006-01-02 15:04:05.000} %{level:.4s} %{shortfunc:16s} ▶ %{id:03x}%{color:reset} %{message}", ) backend1 := logging.NewLogBackend(os.Stderr, "", 0) backend2 := logging.NewLogBackend(os.Stdout, "", 0) backend2Formatter := logging.NewBackendFormatter(backend2, customLogFormat) backend1Leveled := logging.AddModuleLevel(backend1) backend1Leveled.SetLevel(logging.ERROR, "") backend2Leveled := logging.AddModuleLevel(backend2Formatter) backend2Leveled.SetLevel(level, "") // 0=crit 1=err 2=warn 3=notice 4=info 5=debug logging.SetBackend(backend1Leveled, backend2Leveled) return }
////////////////////////////////////////////////////////////////////////////////////////////////////// // 2way of run // - 1st: cmd web // call from brower: http://localhost:8080/main/core/1418,1419,2502,2694,2932,2933,2695 // - 2nd: cmd core/graphite 1418,1419,2502,2694,2932,2933,2695 ////////////////////////////////////////////////////////////////////////////////////////////////////// func main() { LOGFILE, err := os.OpenFile(LOGPATH, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { LOG.Fatalf("Log file error: %s %s", LOGPATH, err) } defer func() { LOGFILE.WriteString(fmt.Sprintf("closing %s", time.UnixDate)) LOGFILE.Close() }() logback := logging.NewLogBackend(LOGFILE, "", 0) logformatted := logging.NewBackendFormatter(logback, LOGFORMAT) loglevel := "DEBUG" GLOGLEVEL, err := logging.LogLevel(loglevel) if err != nil { GLOGLEVEL = logging.DEBUG } logging.SetBackend(logformatted) logging.SetLevel(GLOGLEVEL, "") programName := os.Args[0:1] if len(os.Args) < 2 { HCIDS = append(HCIDS, 1418, 1419, 2502, 2694, 2932, 2933, 2695) mainExec() } else { typeStr := os.Args[1:2] if len(os.Args) >= 3 { hcidStr := os.Args[2:3] allArgs := os.Args[1:] fmt.Println(programName, typeStr, hcidStr, allArgs) arry := strings.Split(hcidStr[0], ",") for i := range arry { n, _ := strconv.Atoi(arry[i]) HCIDS = append(HCIDS, n) } } else { allArgs := os.Args[1:] fmt.Println(programName, typeStr, allArgs) } if typeStr[0] == "web" { webserver() } else { STYPE = typeStr[0] mainExec() } } fmt.Println(HCIDS) }
// SetupLOG sets up logger with the correct parameters for the whole cilium architecture. func SetupLOG(logger *l.Logger, logLevel string) { hostname, _ := os.Hostname() fileFormat := l.MustStringFormatter( `%{time:` + RFC3339Milli + `} ` + hostname + ` %{level:.4s} %{id:03x} %{shortfunc} > %{message}`, ) level, err := l.LogLevel(logLevel) if err != nil { logger.Fatal(err) } backend := l.NewLogBackend(os.Stderr, "", 0) oBF := l.NewBackendFormatter(backend, fileFormat) backendLeveled := l.SetBackend(oBF) backendLeveled.SetLevel(level, "") logger.SetBackend(backendLeveled) }
func BenchmarkGologgingTextPositive(b *testing.B) { stream := &blackholeStream{} logger := log.MustGetLogger("") subBackend := log.NewLogBackend(stream, "", 0) formatter := log.MustStringFormatter("%{time:2006-01-02T15:04:05Z07:00} %{level} %{message}") backend := log.NewBackendFormatter(subBackend, formatter) leveled := log.AddModuleLevel(backend) logger.SetBackend(leveled) 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(b.N) { b.Fatalf("Log write count") } }
func logSetupGlobal(logLevelStr string) { logLevel, err := logging.LogLevel(logLevelStr) if err != nil { log.Fatal("Invalid log level specified") } var formatStdout = logging.MustStringFormatter( "%{color}%{time:2006-01-02T15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{color:reset} %{message}", ) stdout := logging.NewLogBackend(os.Stdout, "", 0) formatter := logging.NewBackendFormatter(stdout, formatStdout) stdoutLeveled := logging.AddModuleLevel(formatter) stdoutLeveled.SetLevel(logLevel, "") syslogBackend, err := logging.NewSyslogBackendPriority("cluegetter", syslog.LOG_MAIL) if err != nil { Log.Fatal(err) } logging.SetBackend(syslogBackend, stdoutLeveled) }
func ConfigureLog(logPath string, headless bool) *gologging.Logger { var log = gologging.MustGetLogger("vamp-router") var backend *gologging.LogBackend var format = gologging.MustStringFormatter( "%{color}%{time:15:04:05.000} %{shortpkg:.4s} %{level:.4s} ==> %{color:reset} %{message}", ) // mix in the Lumberjack logger so we can have rotation on log files if headless { if len(logPath) > 0 { backend = gologging.NewLogBackend(io.MultiWriter(&lumberjack.Logger{ Filename: logPath, MaxSize: 50, // megabytes MaxBackups: 2, //days MaxAge: 14, }), "", 0) } } else { if len(logPath) > 0 { backend = gologging.NewLogBackend(io.MultiWriter(&lumberjack.Logger{ Filename: logPath, MaxSize: 50, // megabytes MaxBackups: 2, //days MaxAge: 14, }, os.Stdout), "", 0) } } backendFormatter := gologging.NewBackendFormatter(backend, format) gologging.SetBackend(backendFormatter) return log }
func main() { startup := StartupConfig{} flag.StringVar(&startup.ConfigFile, "config", "/etc/servicegateway.json", "configuration file") flag.StringVar(&startup.DispatchingMode, "dispatch", "path", "dispatching mode ('path' or 'host')") flag.IntVar(&startup.Port, "port", 8080, "HTTP port to listen on") flag.BoolVar(&startup.Debug, "debug", false, "enable to add debug information to each request") flag.StringVar(&startup.ConsulBaseKey, "consul-base", "gateway/ui", "base key name for configuration") flag.StringVar(&startup.UiDir, "ui-dir", "/usr/share/servicegateway", "directory in which UI files can be found") flag.Parse() logger := logging.MustGetLogger("startup") format := logging.MustStringFormatter("%{color}%{time:15:04:05.000} %{module:12s} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}") backend := logging.NewLogBackend(os.Stderr, "", 0) logging.SetBackend(logging.NewBackendFormatter(backend, format)) logger.Info("Completed startup") cfg := config.Configuration{} data, err := ioutil.ReadFile(startup.ConfigFile) if err != nil { logger.Fatal(err) } err = json.Unmarshal(data, &cfg) if err != nil { logger.Fatal(err) panic(err) } logger.Debug("%s", cfg) consulConfig := api.DefaultConfig() consulConfig.Address = "consul.service.consul:8500" consulConfig.Datacenter = "dev" consulClient, err := api.NewClient(consulConfig) if err != nil { logger.Panic(err) } redisPool := redis.NewPool(func() (redis.Conn, error) { return redis.Dial("tcp", cfg.Redis) }, 8) handler := proxy.NewProxyHandler(logging.MustGetLogger("proxy")) listenAddress := fmt.Sprintf(":%d", startup.Port) done := make(chan bool) go func() { var lastIndex uint64 = 0 var err error dispChan := make(chan dispatcher.Dispatcher) go func() { for disp := range dispChan { logger.Info("starting dispatcher on address %s", listenAddress) manners.ListenAndServe(listenAddress, disp) } }() for { var dispatcher dispatcher.Dispatcher if lastIndex > 0 { time.Sleep(30 * time.Second) } dispatcher, lastIndex, err = buildDispatcher( &startup, &cfg, consulClient, handler, redisPool, logger, lastIndex, ) if err != nil { logger.Error(err.Error()) } else { manners.Close() dispChan <- dispatcher } } }() logger.Info("waiting to die") <-done }
////////////////////////////////////////////////////////////////////////////////////////////////////// // 2way of run // - 1st: graphite_m web // call from brower: http://localhost:8080/main/core/1418,1419,2502,2694,2932,2933,2695 // - 2nd: graphite_m core/graphite 1418,1419,2502,2694,2932,2933,2695 ////////////////////////////////////////////////////////////////////////////////////////////////////// func main() { var logfile = LOGPATH if _, err := os.Stat(LOGPATH); err != nil { LOGPATH, _ := os.Getwd() logfile = LOGPATH + "/graphite_m.log" } LOGFILE, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { LOG.Fatalf("Log file error: %s %s", logfile, err) } defer func() { LOGFILE.WriteString(fmt.Sprintf("closing %s", time.UnixDate)) LOGFILE.Close() }() logback := logging.NewLogBackend(LOGFILE, "", 0) logformatted := logging.NewBackendFormatter(logback, LOGFORMAT) loglevel := "DEBUG" GLOGLEVEL, err := logging.LogLevel(loglevel) if err != nil { GLOGLEVEL = logging.DEBUG } logging.SetBackend(logformatted) logging.SetLevel(GLOGLEVEL, "") // cfg, err := ini.LoadFile(*configfile) // if err != nil { // LOG.Fatalf("parse config "+*configfile+" file error: %s", err) // } // // logfile, ok := cfg.Get("core_api_url", "logfile") // if !ok { // LOG.Fatalf("'logfile' missing from 'system' section") // } DOMAIN = readValConf(CONFIGFILE, "core_api_url") if DOMAIN == "" { DOMAIN = "http://core.local.xdn.com" } programName := os.Args[0:1] if len(os.Args) < 2 { HCIDS = append(HCIDS, 1418, 1419, 2502, 2694, 2932, 2933, 2695) mainExec() } else { typeStr := os.Args[1:2] if len(os.Args) >= 3 { hcidStr := os.Args[2:3] allArgs := os.Args[1:] fmt.Println(programName, typeStr, hcidStr, allArgs) arry := strings.Split(hcidStr[0], ",") for i := range arry { n, _ := strconv.Atoi(arry[i]) HCIDS = append(HCIDS, n) } } else { allArgs := os.Args[1:] fmt.Println(programName, typeStr, allArgs) } if typeStr[0] == "web" { webserver() } else { STYPE = typeStr[0] mainExec() } } fmt.Println(HCIDS) }
func initLogging() { backend := logging.NewLogBackend(os.Stdout, "", 0) formatted := logging.NewBackendFormatter(backend, format) logging.SetBackend(formatted) }
func NewGraphite(method string, key string, args []byte) error { runtime.GOMAXPROCS(runtime.NumCPU()) //parse config log.Debug("%s\n", *Configfile) cfg, err = ini.LoadFile(*Configfile) if err != nil { log.Fatalf("parse config "+*Configfile+" file error: %s", err) } logfile, ok := cfg.Get("system", "logfile") if !ok { log.Fatal("'logfile' missing from 'system' section") } logDir := logfile[0:strings.LastIndex(logfile, "/")] err = os.MkdirAll(logDir, 0777) if err != nil { log.Fatalf("MkdirAll error: %s", err) } //open log file logFile, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { log.Fatalf("%s, %s ", logfile, err) } defer func() { logFile.WriteString(fmt.Sprintf("closing %s", time.UnixDate)) logFile.Close() }() logback := logging.NewLogBackend(logFile, "", 0) logformatted := logging.NewBackendFormatter(logback, logFormat) loglevel, ok := cfg.Get("system", "loglevel") if !ok { log.Fatal("'loglevel' missing from 'system' section") } loglevel = strings.ToUpper(loglevel) Gloglevel, err = logging.LogLevel(loglevel) if err != nil { Gloglevel = logging.DEBUG } logging.SetBackend(logformatted) //see what we have here log.Debug("BUILD: %s\n", Build) for name, section := range cfg { log.Debug("Section: %v\n", name) for k, v := range section { log.Debug("%v: %v\n", k, v) } } host, ok := cfg.Get("server", "host") if !ok { log.Fatal("'server' missing from 'host' section") } var port int port_str, ok := cfg.Get("server", "port") if !ok { log.Fatal("'server' missing from 'port' section") } else { v, err := strconv.Atoi(port_str) if err != nil { log.Warning("'server' missing from 'port' section") } else { port = v } } Graphite, err = graphite.NewGraphite(host, port) log.Debug("Loaded Graphite connection: %#v", Graphite) if method == "simplesend" { var meta map[string]interface{} json.Unmarshal(args, &meta) err = Graphite.SimpleSend(key, getArg(meta, "Value")) log.Debug("Loaded Graphite SimpleSend: %#v", err) } else if method == "sendmetric" { var meta map[string]interface{} json.Unmarshal(args, &meta) Metric.Name = key Metric.Value = getArg(meta, "Value") Metric.Timestamp = time.Now().Unix() err = Graphite.SendMetric(Metric) log.Debug("Loaded Graphite SendMetric: %#v", err) } else if method == "sendmetrics" { metas := make([]map[string]interface{}, 0) json.Unmarshal(args, &metas) log.Debug("metas: %#v", metas) for _, meta := range metas { Metric.Name = key Metric.Value = getArg(meta, "Value") Metric.Timestamp, _ = strconv.ParseInt(getArg(meta, "Date"), 10, 64) Metrics = append(Metrics, Metric) } log.Debug("Metrics: %#v", Metrics) err = Graphite.SendMetrics(Metrics) log.Debug("Loaded Graphite SendMetric: %#v", err) } Graphite.Disconnect() // logging.SetLevel(Gloglevel, "") if err != nil { return err } return nil }
func getLeveledBackend(out io.Writer, formatter logging.Formatter) logging.LeveledBackend { backend := logging.NewLogBackend(out, "", 0) backendFormatter := logging.NewBackendFormatter(backend, formatter) return logging.AddModuleLevel(backendFormatter) }