Skip to content

Commit

Permalink
refactor: logger
Browse files Browse the repository at this point in the history
  • Loading branch information
iseki0 committed Jun 14, 2022
1 parent bab436e commit 9410aa0
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 54 deletions.
3 changes: 3 additions & 0 deletions cmd/idea_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/murphysecurity/murphysec/api"
"github.com/murphysecurity/murphysec/logger"
"github.com/murphysecurity/murphysec/model"
"github.com/murphysecurity/murphysec/utils"
"github.com/murphysecurity/murphysec/utils/must"
Expand All @@ -21,6 +22,8 @@ func reportIdeaErr(e error, message string) {
code = IdeaApiTimeout
} else if errors.Is(e, api.BaseCommonApiError) {
code = IdeaServerRequestFailed
} else if errors.Is(e, logger.ErrCreateLogFileFailed) {
code = IdeaLogFileCreateErr
}
if message == "" {
message = e.Error()
Expand Down
3 changes: 3 additions & 0 deletions cmd/idea_output_err_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
IdeaTokenInvalid IdeaErrCode = 4
IdeaApiTimeout IdeaErrCode = 5
IdeaScanDirInvalid IdeaErrCode = 6
IdeaLogFileCreateErr IdeaErrCode = 7
)

func (code IdeaErrCode) Error() string {
Expand All @@ -34,6 +35,8 @@ func (code IdeaErrCode) Error() string {
return "ApiTimeout"
case IdeaScanDirInvalid:
return "ScanDirInvalid"
case IdeaLogFileCreateErr:
return "LogFileCreateErr"
}
panic(code)
}
6 changes: 5 additions & 1 deletion cmd/idea_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ func ideaScanCmd() *cobra.Command {
SetGlobalExitCode(1)
return
}
logger.InitLogger()
if e := logger.InitLogger(); e != nil {
reportIdeaErr(e, "")
SetGlobalExitCode(10)
return
}
task := model.CreateScanTask(dir, model.TaskKindNormal, model.TaskTypeIdea)
task.ProjectId = ProjectId
ctx := model.WithScanTask(context.TODO(), task)
Expand Down
3 changes: 2 additions & 1 deletion cmd/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/murphysecurity/murphysec/inspector"
"github.com/murphysecurity/murphysec/logger"
"github.com/murphysecurity/murphysec/utils/must"
"github.com/spf13/cobra"
)

Expand All @@ -12,7 +13,7 @@ func scannerCmd() *cobra.Command {
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
dir := args[0]
logger.InitLogger()
must.Must(logger.InitLogger())
inspector.ScannerScan(dir)
},
}
Expand Down
38 changes: 38 additions & 0 deletions logger/log_err.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package logger

import (
"errors"
"fmt"
)

var ErrCreateLogFileFailed = errors.New("create log file failed")
var ErrLogFileDisabled = errors.New("logfile disabled")

type LogErr struct {
Key error
Cause error
}

func (e *LogErr) Is(target error) bool {
return e.Key == target
}

func (e *LogErr) Unwrap() error {
return e.Cause
}

func (e *LogErr) Error() string {
var prefix string
var suffix string
if e.Key != nil {
prefix = fmt.Sprintf("%s: ", e.Key.Error())
}
if e.Cause != nil {
suffix = e.Error()
}
return prefix + suffix
}

func (e *LogErr) String() string {
return e.Error()
}
58 changes: 29 additions & 29 deletions logger/log_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,48 @@ import (
"fmt"
"github.com/mitchellh/go-homedir"
"github.com/murphysecurity/murphysec/utils/must"
"github.com/pkg/errors"
"os"
"path/filepath"
"regexp"
"strconv"
"sync"
"time"
)

var CliLogFilePathOverride string
var DisableLogFile bool

var defaultLogFile = filepath.Join(must.A(homedir.Dir()), ".murphysec", "logs", fmt.Sprintf("%d.log", time.Now().UnixMilli()))
var loggerFile = func() func() *os.File {
o := sync.Once{}
var file *os.File
f := func() *os.File {
o.Do(func() {
if DisableLogFile {
return
}
var logFilePath = defaultLogFile
if CliLogFilePathOverride != "" {
logFilePath = CliLogFilePathOverride
}
if e := os.MkdirAll(filepath.Dir(logFilePath), 0755); e != nil {
panic(errors.Wrap(e, fmt.Sprintf("Create log file dirs failed. %s", e.Error())))
}
_f, e := os.OpenFile(logFilePath, os.O_CREATE+os.O_RDWR+os.O_APPEND, 0644)
if e != nil {
panic(errors.Wrap(e, fmt.Sprintf("Open log file failed. %s, %s", logFilePath, e.Error())))
}
file = _f
})
return file

func CreateLogFile() (*os.File, error) {
if DisableLogFile {
return nil, ErrLogFileDisabled
}
logFilePath := defaultLogFile
if CliLogFilePathOverride != "" {
logFilePath = CliLogFilePathOverride
}
return f
}()
// ensure log dir created
if e := os.MkdirAll(filepath.Dir(logFilePath), 0755); e != nil {
return nil, &LogErr{
Key: ErrCreateLogFileFailed,
Cause: e,
}
}
if f, e := os.OpenFile(logFilePath, os.O_CREATE+os.O_RDWR+os.O_APPEND, 0644); e != nil {
return nil, &LogErr{
Key: ErrCreateLogFileFailed,
Cause: e,
}
} else {
return f, nil
}
}

// file before staticRefTime will be ignored
var staticRefTime = must.A(time.Parse(time.RFC3339, "2020-01-01T00:00:00Z"))

// LogFileCleanup auto remove log files which created between staticRefTime and 7 days ago
func LogFileCleanup() {
refTime, _ := time.Parse(time.RFC3339, "2020-01-01T00:00:00Z")
logFilePattern := regexp.MustCompile("^(\\d+)\\.log$")
basePath := filepath.Dir(defaultLogFile)
if basePath == "" {
Expand All @@ -64,10 +65,9 @@ func LogFileCleanup() {
continue
}
lt := time.UnixMilli(int64(ts))
if lt.Before(refTime) {
if lt.Before(staticRefTime) {
continue
}

if time.Now().Sub(time.UnixMilli(int64(ts))) > time.Hour*24*7 {
_ = os.Remove(filepath.Join(basePath, entry.Name()))
}
Expand Down
61 changes: 38 additions & 23 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logger

import (
"fmt"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
Expand Down Expand Up @@ -37,31 +38,30 @@ func w(logger *zap.Logger, f func(msg string, fields ...zap.Field)) *WrappedLogg
}
}

func InitLogger() {
encoderConfig := zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
TimeKey: "time",
NameKey: "name",
CallerKey: "caller",
FunctionKey: "",
StacktraceKey: "stacktrace",
SkipLineEnding: false,
LineEnding: "",
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.RFC3339TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeName: nil,
NewReflectedEncoder: nil,
ConsoleSeparator: " ",
}
encoder := zapcore.NewConsoleEncoder(encoderConfig)
fileCore := zapcore.NewCore(encoder, zapcore.Lock(loggerFile()), zapcore.DebugLevel)
var loggerEncoderConfig = zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
TimeKey: "time",
NameKey: "name",
CallerKey: "caller",
FunctionKey: "",
StacktraceKey: "stacktrace",
SkipLineEnding: false,
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.RFC3339TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeName: nil,
NewReflectedEncoder: nil,
ConsoleSeparator: " ",
}

func InitLogger() error {
// console logger
encoder := zapcore.NewConsoleEncoder(loggerEncoderConfig)
consoleCore := zapcore.NewNopCore()
switch strings.ToLower(strings.TrimSpace(ConsoleLogLevelOverride)) {
case "silent":
consoleCore = zapcore.NewNopCore()
case "error":
consoleCore = zapcore.NewCore(encoder, zapcore.Lock(os.Stderr), zapcore.ErrorLevel)
case "warn":
Expand All @@ -71,6 +71,19 @@ func InitLogger() {
case "debug":
consoleCore = zapcore.NewCore(encoder, zapcore.Lock(os.Stderr), zapcore.DebugLevel)
}

// file logger
var fileCore zapcore.Core
logFile, e := CreateLogFile()
if e == nil {
fileCore = zapcore.NewCore(encoder, logFile, zapcore.DebugLevel)
} else if errors.Is(e, ErrLogFileDisabled) {
fileCore = zapcore.NewNopCore()
} else {
return e
}

// all
core := zapcore.NewTee(fileCore, consoleCore)

logger := zap.New(core, zap.AddCaller())
Expand All @@ -82,6 +95,8 @@ func InitLogger() {
Logger = logger.Sugar()

NetworkLogger = zap.NewNop().Sugar()

return nil
}

var Debug = noOp
Expand Down

0 comments on commit 9410aa0

Please sign in to comment.