...
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 if logEnv == "production" {
48 stderrBackendFormatted := logging.NewBackendFormatter(stderrBackend, logTerminalProductionFormat)
49 stderrBackendLeveled := logging.AddModuleLevel(stderrBackendFormatted)
50 stderrBackendLeveled.SetLevel(logging.INFO, "")
51 logging.SetBackend(stderrBackendLeveled)
52 } else {
53 stdoutBackendFormatted := logging.NewBackendFormatter(stdoutBackend, logTerminalFormat)
54 logging.SetBackend(stdoutBackendFormatted)
55 }
56
57 Log.Infof("Logging init successfully with env: %s", logEnv)
58
59
67 }
68
69 func GetEnv() string {
70 return logEnv
71 }
72
View as plain text