github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/log/helper.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package log 7 8 import ( 9 golog "log" 10 "os" 11 "path" 12 13 "github.com/gitbundle/modules/json" 14 ) 15 16 // mode can be console, file 17 func NewSimpleLogger(logBufferLength int64, mode, level, stacktraceLevel, logPath string, noCaller bool) { 18 lvl := INFO 19 if level != "" { 20 lvl = FromString(level) 21 } 22 23 stacktraceLvl := NONE 24 if stacktraceLevel != "" { 25 stacktraceLvl = FromString(stacktraceLevel) 26 } 27 28 logConfig := map[string]interface{}{ 29 "level": lvl.String(), 30 "expression": "", 31 "prefix": "", 32 "flags": LstdFlags, 33 "stacktraceLevel": stacktraceLvl.String(), 34 } 35 36 // Generate log configuration. 37 switch mode { 38 case "console": 39 logConfig["stderr"] = true 40 logConfig["colorize"] = CanColorStderr 41 case "file": 42 if err := os.MkdirAll(path.Dir(logPath), os.ModePerm); err != nil { 43 panic(err.Error()) 44 } 45 46 logConfig["filename"] = logPath 47 logConfig["rotate"] = true 48 logConfig["maxsize"] = 1 << 28 49 logConfig["daily"] = true 50 logConfig["maxdays"] = 7 51 logConfig["compress"] = true 52 logConfig["compressionLevel"] = -1 53 logConfig["colorize"] = false 54 } 55 56 byteConfig, err := json.Marshal(logConfig) 57 if err != nil { 58 panic(err) 59 } 60 61 NewLogger(logBufferLength, mode, mode, string(byteConfig), noCaller) 62 63 // Finally redirect the default golog to here 64 golog.SetFlags(0) 65 golog.SetPrefix("") 66 golog.SetOutput(NewLoggerAsWriter("INFO", GetLogger(DEFAULT))) 67 }