Skip to content

Commit

Permalink
Add log output option of config
Browse files Browse the repository at this point in the history
1.Add log output option of config.
2.Minimal optimization code.
  • Loading branch information
wind-c committed Oct 15, 2023
1 parent 68d0489 commit 5f00772
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 185 deletions.
64 changes: 46 additions & 18 deletions cluster/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,43 @@ package log

import (
"context"
"gopkg.in/natefinch/lumberjack.v2"
"io"
"log/slog"
"os"

"gopkg.in/natefinch/lumberjack.v2"
)

// Constants for log formats
const (
Text = iota // Log format is TEXT.
Json // Log format is JSON.
FormatText = iota // Log format is TEXT.
FormatJson // Log format is JSON.
)

// Constants for log output
const (
OutputConsole = iota // Log output is Console.
OutputFile // Log output is File.
OutputBoth // Log output is Console and File.
)

// Format represents the log format type.
type Format int

// Output represents the log output type.
type Output int

// Options defines configuration options for the logger.
type Options struct {

// Indicates whether logging is enabled.
Disable bool `json:"disable" yaml:"disable"`
Enable bool `json:"enable" yaml:"enable"`

// Log format, currently supports Text: 0 and JSON: 1, with Text as the default.
Format Format `json:"format" yaml:"format"`

// Log output location Console: 0 or File: 1 or Both: 2, with Console as the default.
Output Output `json:"output" yaml:"output"`

// Log level, with supported values LevelDebug: 4, LevelInfo: 0, LevelWarn: 4, and LevelError: 8.
Level int `json:"level" yaml:"level"`

Expand Down Expand Up @@ -61,7 +73,8 @@ func DefaultOptions() *Options {
MaxSize: 100,
MaxAge: 30,
MaxBackups: 1,
Format: Text,
Format: FormatText,
Output: OutputConsole,
}
}

Expand All @@ -72,24 +85,39 @@ func New(opt *Options) *Logger {
}

var writer io.Writer
writer = os.Stdout
switch opt.Output {
case OutputConsole:
writer = os.Stdout
case OutputFile:
writer = createFileWriter(opt)
case OutputBoth:
if fileWriter := createFileWriter(opt); fileWriter != nil {
writer = io.MultiWriter(os.Stdout, fileWriter)
}
}

if writer == nil {
writer = os.Stdout
}

return &Logger{
writer: writer,
Logger: slog.New(NewHandler(opt, writer)),
opt: opt,
}
}

func createFileWriter(opt *Options) (writer io.Writer) {
if len(opt.Filename) != 0 {
fileWriter := &lumberjack.Logger{
writer = &lumberjack.Logger{
Filename: opt.Filename,
MaxSize: opt.MaxSize,
MaxBackups: opt.MaxBackups,
MaxAge: opt.MaxAge,
Compress: opt.Compress,
}
writer = io.MultiWriter(os.Stdout, fileWriter)
}

return &Logger{
writer: writer,
Logger: slog.New(NewHandler(opt, writer)),
opt: opt,
}
return
}

// Logger is a wrapper for slog.Logger.
Expand All @@ -110,12 +138,12 @@ func NewHandler(opt *Options, writer io.Writer) *Handler {
var handler slog.Handler

switch opt.Format {
case Text:
case FormatText:
handler = slog.NewTextHandler(writer, &slog.HandlerOptions{
Level: slog.Level(opt.Level),
})

case Json:
case FormatJson:
handler = slog.NewJSONHandler(writer, &slog.HandlerOptions{
Level: slog.Level(opt.Level),
})
Expand All @@ -135,7 +163,7 @@ func NewHandler(opt *Options, writer io.Writer) *Handler {
// Enabled reports whether the handler handles records at the given level.
// The handler ignores records whose level is lower.
func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool {
return !h.opt.Disable && h.internal.Enabled(ctx, level)
return h.opt.Enable && h.internal.Enabled(ctx, level)
}

// Handle handles the Record.
Expand Down
15 changes: 9 additions & 6 deletions cmd/cluster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ func realMain(ctx context.Context) error {
flag.StringVar(&cfg.Redis.Options.Addr, "redis", "127.0.0.1:6379", "redis address for cluster mode")
flag.StringVar(&cfg.Redis.Options.Password, "redis-pass", "", "redis password for cluster mode")
flag.IntVar(&cfg.Redis.Options.DB, "redis-db", 0, "redis db for cluster mode")
flag.BoolVar(&cfg.Log.Disable, "log-disable", false, "log disabled or not")
flag.BoolVar(&cfg.Log.Enable, "log-enable", true, "log enabled or not")
flag.StringVar(&cfg.Log.Filename, "log-file", "./logs/comqtt.log", "log filename")
//parse arguments
flag.Parse()
//load config file
if confFile != "" {
if len(confFile) > 0 {
if cfg, err = config.Load(confFile); err != nil {
return fmt.Errorf("load config file error: %w", err)
}
Expand All @@ -92,14 +92,17 @@ func realMain(ctx context.Context) error {
}
}

//init log
log.Init(&cfg.Log)

//enable pprof
if cfg.PprofEnable {
pprof()
}

//init log
log.Init(&cfg.Log)
if cfg.Log.Enable && cfg.Log.Output == log.OutputFile {
fmt.Println("log output to the files, please check")
}

// create server instance and init hooks
cfg.Mqtt.Options.Logger = log.Default()
server := mqtt.New(&cfg.Mqtt.Options)
Expand Down Expand Up @@ -149,7 +152,7 @@ func realMain(ctx context.Context) error {
errCh <- err
}
}()
log.Info("comqtt server started")
log.Info("cluster node started")

// exit
select {
Expand Down
25 changes: 9 additions & 16 deletions cmd/config/node1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,12 @@ redis:
prefix: comqtt

log:
enable: true
env: 0 #0 dev or 1 prod
format: 0 #output format 0console or 1json
caller: false #whether to display code line number
info-file: ./logs/comqtt-info.log
error-file: ./logs/comqtt-error.log # level >= 3, 3Error(default) 4Fatal 5Panic 6NoLevel 7Off
thirdparty-file: ./logs/thirdparty.log # level 6NoLevel, logs of the third-party library
maxsize: 100 #100M
max-age: 30 #30day
max-backups: 10 #number of log files
localtime: true #true or false
compress: true #true or false
level: 1 #-1Trace 0Debug 1Info 2Warn 3Error(default) 4Fatal 5Panic 6NoLevel 7Off
sampler: #a maximum of three logs can be output every second
burst: 3 #log count
period: 1 #second
enable: true #Indicates whether logging is enabled.
format: 0 #Log format, currently supports Text: 0 and JSON: 1, with Text as the default.
output: 2 #Log output location Console: 0 or File: 1 or Both: 2, with Console as the default.
filename: ./logs/comqtt1.log #Filename is the file to write logs to
maxsize: 100 #MaxSize is the maximum size in megabytes of the log file before it gets rotated. It defaults to 100 megabytes.
max-age: 30 #MaxAge is the maximum number of days to retain old log files based on the timestamp encoded in their filename
max-backups: 10 #MaxBackups is the maximum number of old log files to retain
compress: true #Compress determines if the rotated log files should be compressed using gzip
level: 0 #Log level, with supported values LevelDebug: -4, LevelInfo: 0, LevelWarn: 4, and LevelError: 8.
25 changes: 9 additions & 16 deletions cmd/config/node2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,12 @@ redis:
prefix: comqtt

log:
enable: true
env: 0 #0 dev or 1 prod
format: 0 #output format 0console or 1json
caller: false #whether to display code line number
info-file: ./logs/comqtt-info.log
error-file: ./logs/comqtt-error.log
thirdparty-file: ./logs/thirdparty.log # level 6NoLevel, logs of the third-party library
maxsize: 100 #100M
max-age: 30 #30day
max-backups: 10 #number of log files
localtime: true #true or false
compress: true #true or false
level: 1 #-1Trace 0Debug 1Info 2Warn 3Error(default) 4Fatal 5Panic 6NoLevel 7Off
sampler: #a maximum of three logs can be output every second
burst: 3 #log count
period: 1 #second
enable: true #Indicates whether logging is enabled.
format: 0 #Log format, currently supports Text: 0 and JSON: 1, with Text as the default.
output: 2 #Log output location Console: 0 or File: 1 or Both: 2, with Console as the default.
filename: ./logs/comqtt2.log #Filename is the file to write logs to
maxsize: 100 #MaxSize is the maximum size in megabytes of the log file before it gets rotated. It defaults to 100 megabytes.
max-age: 30 #MaxAge is the maximum number of days to retain old log files based on the timestamp encoded in their filename
max-backups: 10 #MaxBackups is the maximum number of old log files to retain
compress: true #Compress determines if the rotated log files should be compressed using gzip
level: 0 #Log level, with supported values LevelDebug: -4, LevelInfo: 0, LevelWarn: 4, and LevelError: 8.
25 changes: 9 additions & 16 deletions cmd/config/node3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,12 @@ redis:
prefix: comqtt

log:
enable: true
env: 0 #0 dev or 1 prod
format: 0 #output format 0console or 1json
caller: false #whether to display code line number
info-file: ./logs/comqtt-info.log
error-file: ./logs/comqtt-error.log
thirdparty-file: ./logs/thirdparty.log # level 6NoLevel, logs of the third-party library
maxsize: 100 #100M
max-age: 30 #30day
max-backups: 10 #number of log files
localtime: true #true or false
compress: true #true or false
level: 1 #-1Trace 0Debug 1Info 2Warn 3Error(default) 4Fatal 5Panic 6NoLevel 7Off
sampler: #a maximum of three logs can be output every second
burst: 3 #log count
period: 1 #second
enable: true #Indicates whether logging is enabled.
format: 0 #Log format, currently supports Text: 0 and JSON: 1, with Text as the default.
output: 2 #Log output location Console: 0 or File: 1 or Both: 2, with Console as the default.
filename: ./logs/comqtt3.log #Filename is the file to write logs to
maxsize: 100 #MaxSize is the maximum size in megabytes of the log file before it gets rotated. It defaults to 100 megabytes.
max-age: 30 #MaxAge is the maximum number of days to retain old log files based on the timestamp encoded in their filename
max-backups: 10 #MaxBackups is the maximum number of old log files to retain
compress: true #Compress determines if the rotated log files should be compressed using gzip
level: 0 #Log level, with supported values LevelDebug: -4, LevelInfo: 0, LevelWarn: 4, and LevelError: 8.
85 changes: 0 additions & 85 deletions cmd/config/node4.yml

This file was deleted.

24 changes: 9 additions & 15 deletions cmd/config/single.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,12 @@ redis:
prefix: comqtt

log:
enable: true
env: 0 #0 dev or 1 prod
format: 1 #output format 0console or 1json
caller: false #whether to display code line number
info-file: ./logs/comqtt-info.log
error-file: ./logs/comqtt-error.log # level >= 3, 3Error(default) 4Fatal 5Panic 6NoLevel 7Off
maxsize: 100 #100M
max-age: 30 #30day
max-backups: 10 #number of log files
localtime: true #true or false
compress: true #true or false
level: 1 #-1Trace 0Debug 1Info 2Warn 3Error(default) 4Fatal 5Panic 6NoLevel 7Off
sampler: #a maximum of three logs can be output every second
burst: 3 #log count
period: 1 #second
enable: true #Indicates whether logging is enabled.
format: 1 #Log format, currently supports Text: 0 and JSON: 1, with Text as the default.
output: 2 #Log output location Console: 0 or File: 1 or Both: 2, with Console as the default.
filename: ./logs/comqtt.log #Filename is the file to write logs to
maxsize: 100 #MaxSize is the maximum size in megabytes of the log file before it gets rotated. It defaults to 100 megabytes.
max-age: 30 #MaxAge is the maximum number of days to retain old log files based on the timestamp encoded in their filename
max-backups: 10 #MaxBackups is the maximum number of old log files to retain
compress: true #Compress determines if the rotated log files should be compressed using gzip
level: 0 #Log level, with supported values LevelDebug: -4, LevelInfo: 0, LevelWarn: 4, and LevelError: 8.
Loading

0 comments on commit 5f00772

Please sign in to comment.