Skip to content

Commit

Permalink
kohaku 移植
Browse files Browse the repository at this point in the history
  • Loading branch information
voluntas committed Aug 22, 2023
1 parent 9b81c0e commit 12057ef
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
40 changes: 40 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
}
Expand Down
23 changes: 12 additions & 11 deletions logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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 ""
Expand All @@ -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)
}
Expand Down

0 comments on commit 12057ef

Please sign in to comment.