From 12057efa16d1f6cfcc3f3173c5734d342d3d3ea7 Mon Sep 17 00:00:00 2001 From: voluntas Date: Tue, 22 Aug 2023 14:31:12 +0900 Subject: [PATCH] =?UTF-8?q?kohaku=20=E7=A7=BB=E6=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.go | 40 ++++++++++++++++++++++++++++++++++++++++ logging.go | 23 ++++++++++++----------- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/config.go b/config.go index 7992b88..45dc9b7 100644 --- a/config.go +++ b/config.go @@ -8,6 +8,18 @@ import ( ) const ( + DefaultLogDir = "." + DefaultLogName = "suzu.jsonl" + + // megabytes + DefaultLogRotateMaxSize = 200 + DefaultLogRotateMaxBackups = 7 + // days + DefaultLogRotateMaxAge = 30 + + DefaultExporterListenAddr = "0.0.0.0" + DefaultExporterListenPort = 5891 + // 100ms DefaultTimeToWaitForOpusPacketMs = 100 ) @@ -109,6 +121,34 @@ func NewConfig(configFilePath string) (*Config, error) { } func setDefaultsConfig(config *Config) { + if config.LogDir == "" { + config.LogDir = DefaultLogDir + } + + if config.LogName == "" { + config.LogDir = DefaultLogName + } + + if config.LogRotateMaxSize == 0 { + config.LogRotateMaxSize = DefaultLogRotateMaxSize + } + + if config.LogRotateMaxBackups == 0 { + config.LogRotateMaxBackups = DefaultLogRotateMaxBackups + } + + if config.LogRotateMaxAge == 0 { + config.LogRotateMaxAge = DefaultLogRotateMaxAge + } + + if config.ExporterListenAddr == "" { + config.ExporterListenAddr = DefaultExporterListenAddr + } + + if config.ExporterListenPort == 0 { + config.ExporterListenPort = DefaultExporterListenPort + } + if config.TimeToWaitForOpusPacketMs == 0 { config.TimeToWaitForOpusPacketMs = DefaultTimeToWaitForOpusPacketMs } diff --git a/logging.go b/logging.go index 0ae69f5..0394d93 100644 --- a/logging.go +++ b/logging.go @@ -12,14 +12,6 @@ import ( "gopkg.in/natefinch/lumberjack.v2" ) -const ( - // megabytes - DefaultLogRotateMaxSize = 200 - DefaultLogRotateMaxBackups = 7 - // days - DefaultLogRotateMaxAge = 30 -) - // InitLogger ロガーを初期化する func InitLogger(config *Config) error { if f, err := os.Stat(config.LogDir); os.IsNotExist(err) || !f.IsDir() { @@ -33,7 +25,7 @@ func InitLogger(config *Config) error { return time.Now().UTC() } - zerolog.TimeFieldFormat = time.RFC3339Nano + zerolog.TimeFieldFormat = "2006-01-02T15:04:05.000000Z" if config.Debug { zerolog.SetGlobalLevel(zerolog.DebugLevel) @@ -51,7 +43,7 @@ func InitLogger(config *Config) error { }, NoColor: false, } - format(&writer) + prettyFormat(&writer) log.Logger = zerolog.New(writer).With().Caller().Timestamp().Logger() } else if config.LogStdout { writer := os.Stdout @@ -81,11 +73,15 @@ func InitLogger(config *Config) error { return nil } -func format(w *zerolog.ConsoleWriter) { +// 現時点での prettyFormat +// 2023-04-17 12:51:56.333485Z [INFO] config.go:102 > CONF | debug=true +func prettyFormat(w *zerolog.ConsoleWriter) { const Reset = "\x1b[0m" w.FormatLevel = func(i interface{}) string { var color, level string + // TODO: 各色を定数に置き換える + // TODO: 他の logLevel が必要な場合は追加する switch i.(string) { case "info": color = "\x1b[32m" @@ -105,6 +101,10 @@ func format(w *zerolog.ConsoleWriter) { w.FormatCaller = func(i interface{}) string { return fmt.Sprintf("[%s]", filepath.Base(i.(string))) } + // TODO: Caller をファイル名と行番号だけの表示で出力する + // 以下のようなフォーマットにしたい + // 2023-04-17 12:50:09.334758Z [INFO] [config.go:102] CONF | debug=true + // TODO: name=value が無い場合に | を消す方法がわからなかった w.FormatMessage = func(i interface{}) string { if i == nil { return "" @@ -116,6 +116,7 @@ func format(w *zerolog.ConsoleWriter) { const Cyan = "\x1b[36m" return fmt.Sprintf("%s%s=%s", Cyan, i, Reset) } + // TODO: カンマ区切りを同実現するかわからなかった w.FormatFieldValue = func(i interface{}) string { return fmt.Sprintf("%s", i) }