2023-03-22 22:45:17 +08:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"octopus"
|
|
|
|
"octopus/internal/config"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/rs/zerolog/pkgerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Setup() {
|
|
|
|
cfg := config.Get()
|
|
|
|
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
|
|
|
|
|
|
|
|
writers := []io.Writer{}
|
2023-03-23 21:27:28 +08:00
|
|
|
// if config.Get().IsLocal {
|
|
|
|
writers = append(writers, zerolog.NewConsoleWriter())
|
|
|
|
// } else {
|
|
|
|
// writers = append(writers, os.Stderr)
|
|
|
|
// }
|
2023-03-22 22:45:17 +08:00
|
|
|
if dsn := cfg.Sentry.DSN; dsn != "" {
|
|
|
|
sentryWriter, err := NewSentryWriter(
|
|
|
|
cfg.Sentry.DSN,
|
|
|
|
WithDebug(cfg.Debug),
|
|
|
|
WithEnvironment(cfg.Sentry.Environment),
|
|
|
|
WithRelease(octopus.Version),
|
|
|
|
WithServerName("octopus-service"),
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
writers = append(writers, sentryWriter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Logger = zerolog.New(io.MultiWriter(writers...)).With().Stack().Timestamp().Logger()
|
|
|
|
}
|