MCPcopy
hub / github.com/canopy-network/canopy / NewLogger

Function NewLogger

lib/log.go:202–237  ·  view source on GitHub ↗

NewLogger() creates a new Logger instance with the specified configuration and optional data directory path

(config LoggerConfig, dataDirPath ...string)

Source from the content-addressed store, hash-verified

200
201// NewLogger() creates a new Logger instance with the specified configuration and optional data directory path
202func NewLogger(config LoggerConfig, dataDirPath ...string) LoggerI {
203 if config.Out == nil {
204 if dataDirPath == nil || dataDirPath[0] == "" {
205 dataDirPath = make([]string, 1)
206 dataDirPath[0] = DefaultDataDirPath()
207 }
208 logPath := filepath.Join(dataDirPath[0], LogDirectory, LogFileName)
209 if _, err := os.Stat(logPath); errors.Is(err, os.ErrNotExist) {
210 if err = os.MkdirAll(filepath.Join(dataDirPath[0], LogDirectory), os.ModePerm); err != nil {
211 panic(err)
212 }
213 }
214 logFile := &lumberjack.Logger{
215 Filename: logPath,
216 MaxSize: 1, // megabyte
217 MaxBackups: 1500,
218 MaxAge: 14, // days
219 Compress: true,
220 }
221 config.Out = io.MultiWriter(os.Stdout, logFile)
222 }
223 var slogger *slog.Logger
224 if config.JSON {
225 slogger = slog.New(slog.NewJSONHandler(config.Out, &slog.HandlerOptions{
226 Level: slog.Level(config.Level),
227 }))
228 } else {
229 slogger = slog.New(slog.NewTextHandler(config.Out, &slog.HandlerOptions{
230 Level: slog.Level(config.Level),
231 }))
232 }
233 return &Logger{
234 config: config,
235 slog: slogger,
236 }
237}
238
239// NewDefaultLogger() creates a Logger with default settings, logging at the Debug level to stdout
240func NewDefaultLogger() LoggerI {

Callers 6

initFunction · 0.92
getConfigsFunction · 0.92
TestNewDefaultLoggerFunction · 0.85
TestNewNullLoggerFunction · 0.85
NewDefaultLoggerFunction · 0.85
NewNullLoggerFunction · 0.85

Calls 2

DefaultDataDirPathFunction · 0.85
NewMethod · 0.65

Tested by 2

TestNewDefaultLoggerFunction · 0.68
TestNewNullLoggerFunction · 0.68