Skip to content

Commit

Permalink
file log (#627)
Browse files Browse the repository at this point in the history
* update cache

* support file log
  • Loading branch information
yuyang0 committed Nov 30, 2023
1 parent e7ba647 commit 4066bf7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion core.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func serve(c *cli.Context) error {
zerolog.Fatal().Err(err).Send()
}

if err := log.SetupLog(c.Context, config.LogLevel, config.SentryDSN); err != nil {
if err := log.SetupLog(c.Context, &config.Log, config.SentryDSN); err != nil {
zerolog.Fatal().Err(err).Send()
}
defer log.SentryDefer()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ require (
golang.org/x/sync v0.1.0
google.golang.org/grpc v1.54.1
google.golang.org/protobuf v1.30.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

require (
Expand Down Expand Up @@ -142,7 +143,6 @@ require (
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.8.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
29 changes: 23 additions & 6 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package log

import (
"context"
"io"
"os"
"strings"
"time"

"github.com/cockroachdb/errors"
"github.com/getsentry/sentry-go"
"github.com/projecteru2/core/types"
"gopkg.in/natefinch/lumberjack.v2"

"github.com/rs/zerolog"
)
Expand All @@ -18,17 +21,31 @@ var (
)

// SetupLog init logger
func SetupLog(ctx context.Context, l, dsn string) error {
level, err := zerolog.ParseLevel(strings.ToLower(l))
func SetupLog(ctx context.Context, cfg *types.ServerLogConfig, dsn string) error {
level, err := zerolog.ParseLevel(strings.ToLower(cfg.Level))
if err != nil {
return err
}
// TODO can use file
rslog := zerolog.New(
zerolog.ConsoleWriter{

var writer io.Writer
switch {
case cfg.Filename != "":
// file log always uses json format
writer = &lumberjack.Logger{
Filename: cfg.Filename,
MaxBackups: cfg.MaxBackups, // files
MaxSize: cfg.MaxSize, // megabytes
MaxAge: cfg.MaxAge, // days
}
case !cfg.UseJSON:
writer = zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: time.RFC822,
}).With().Timestamp().Logger()
}
default:
writer = os.Stdout
}
rslog := zerolog.New(writer).With().Timestamp().Logger()
rslog.Level(level)
zerolog.ErrorStackMarshaler = func(err error) any {
return errors.GetSafeDetails(err).SafeDetails
Expand Down
2 changes: 2 additions & 0 deletions resource/cobalt/cobalt.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (m *Manager) LoadPlugins(ctx context.Context, t *testing.T) error {
if _, ok := cache[b.Name()]; ok {
continue
}
cache[b.Name()] = struct{}{}
m.AddPlugins(b)
}

Expand All @@ -80,6 +81,7 @@ func (m *Manager) LoadPlugins(ctx context.Context, t *testing.T) error {
if _, ok := cache[b.Name()]; ok {
continue
}
cache[b.Name()] = struct{}{}
m.AddPlugins(b)
}
return nil
Expand Down
12 changes: 11 additions & 1 deletion types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const (

// Config holds eru-core config
type Config struct {
LogLevel string `yaml:"log_level" required:"true" default:"INFO"`
Bind string `yaml:"bind" required:"true" default:"5001"` // HTTP API address
LockTimeout time.Duration `yaml:"lock_timeout" required:"true" default:"30s"` // timeout for lock (ttl)
GlobalTimeout time.Duration `yaml:"global_timeout" required:"true" default:"300s"` // timeout for remove, run_and_wait and build, in second
Expand All @@ -44,6 +43,7 @@ type Config struct {
Systemd SystemdConfig `yaml:"systemd"`
Scheduler SchedulerConfig `yaml:"scheduler"`
ResourcePlugin ResourcePluginConfig `yaml:"resource_plugin"`
Log ServerLogConfig `yaml:"log"`
}

// Identifier returns the ID of this config
Expand Down Expand Up @@ -144,3 +144,13 @@ type LogConfig struct {
Type string `yaml:"type" required:"true" default:"journald"` // Log type, can be "journald", "json-file", "none"
Config map[string]string `yaml:"config"` // Log configs
}

type ServerLogConfig struct {
Level string `yaml:"level" default:"info"`
UseJSON bool `yaml:"use_json"`
// for file log
Filename string `yaml:"filename"`
MaxSize int `yaml:"maxsize" default:"500"`
MaxAge int `yaml:"max_age" default:"28"`
MaxBackups int `yaml:"max_backups" default:"3"`
}

0 comments on commit 4066bf7

Please sign in to comment.