...
1
18
19
20 package log
21
22 import (
23 "github.com/op/go-logging"
24 "os"
25 )
26
27 var logEnv = "production"
28
29
30 var Log = logging.MustGetLogger("scheduler")
31
32 var logTerminalFormat = logging.MustStringFormatter(
33 `%{color}%{time} %{level:.4s}/%{shortpkg}.%{shortfunc}%{color:reset} %{message}`,
34 )
35 var logTerminalProductionFormat = logging.MustStringFormatter(
36 `%{time} %{shortfunc} > %{level:.4s} %{id:03x} %{message}`,
37 )
38
39 func init() {
40 stdoutBackend := logging.NewLogBackend(os.Stdout, "", 0)
41 stderrBackend := logging.NewLogBackend(os.Stderr, "", 0)
42
43 if os.Getenv("P2PFAAS_LOG_ENV") == "development" {
44 logEnv = "development"
45 }
46
47
48 if logEnv == "production" {
49 stderrBackendFormatted := logging.NewBackendFormatter(stderrBackend, logTerminalProductionFormat)
50 stderrBackendLeveled := logging.AddModuleLevel(stderrBackendFormatted)
51 stderrBackendLeveled.SetLevel(logging.INFO, "")
52 logging.SetBackend(stderrBackendLeveled)
53 } else {
54 stdoutBackendFormatted := logging.NewBackendFormatter(stdoutBackend, logTerminalFormat)
55 logging.SetBackend(stdoutBackendFormatted)
56 }
57
58
66
67 Log.Infof("Logging init successfully with env: %s", logEnv)
68 }
69
70 func GetEnv() string {
71 return logEnv
72 }
73
View as plain text