| 11 | var Logger *zap.SugaredLogger = initLogger(false) |
| 12 | |
| 13 | func initLogger(enableStackTrace bool) *zap.SugaredLogger { |
| 14 | config := zap.NewProductionEncoderConfig() |
| 15 | config.EncodeTime = zapcore.ISO8601TimeEncoder |
| 16 | |
| 17 | fileEncoder := zapcore.NewJSONEncoder(config) |
| 18 | stdoutEncoder := zapcore.NewConsoleEncoder(config) |
| 19 | |
| 20 | // FIXME read from env variable |
| 21 | logFile, err := os.OpenFile( |
| 22 | "github-analyzer.log", |
| 23 | os.O_APPEND|os.O_CREATE|os.O_WRONLY, |
| 24 | 0644, |
| 25 | ) |
| 26 | if err != nil { |
| 27 | log.Fatal(err) |
| 28 | } |
| 29 | |
| 30 | writer := zapcore.AddSync(logFile) |
| 31 | defaultLogLevel := zapcore.DebugLevel |
| 32 | |
| 33 | core := zapcore.NewTee( |
| 34 | zapcore.NewCore( |
| 35 | stdoutEncoder, |
| 36 | zapcore.AddSync(os.Stdout), |
| 37 | defaultLogLevel, |
| 38 | ), |
| 39 | zapcore.NewCore(fileEncoder, writer, defaultLogLevel), |
| 40 | ) |
| 41 | |
| 42 | if enableStackTrace { |
| 43 | return zap.New(core, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel)). |
| 44 | Sugar() |
| 45 | } |
| 46 | return zap.New(core, zap.AddCaller()).Sugar() |
| 47 | } |