mosdns/mlog/logger.go
dengxiongjian cd761e8145
Some checks are pending
Test mosdns / build (push) Waiting to run
新增Mikrotik API 插入解析ip
2025-07-31 11:28:55 +08:00

92 lines
2.2 KiB
Go

/*
* Copyright (C) 2020-2022, IrineSistiana
*
* This file is part of mosdns.
*
* mosdns is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mosdns is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mlog
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
)
type LogConfig struct {
// Level, See also zapcore.ParseLevel.
Level string `yaml:"level"`
// File that logger will be writen into.
// Default is stderr.
File string `yaml:"file"`
// Production enables json output.
Production bool `yaml:"production"`
}
var (
stderr = zapcore.Lock(os.Stderr)
lvl = zap.NewAtomicLevelAt(zap.InfoLevel)
l = zap.New(zapcore.NewCore(zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()), stderr, lvl))
s = l.Sugar()
nop = zap.NewNop()
)
func NewLogger(lc LogConfig) (*zap.Logger, error) {
lvl, err := zapcore.ParseLevel(lc.Level)
if err != nil {
return nil, fmt.Errorf("invalid log level: %w", err)
}
var out zapcore.WriteSyncer
if lf := lc.File; len(lf) > 0 {
f, _, err := zap.Open(lf)
if err != nil {
return nil, fmt.Errorf("open log file: %w", err)
}
out = zapcore.Lock(f)
} else {
out = stderr
}
if lc.Production {
return zap.New(zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), out, lvl)), nil
}
return zap.New(zapcore.NewCore(zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()), out, lvl)), nil
}
// L is a global logger.
func L() *zap.Logger {
return l
}
// SetLevel sets the log level for the global logger.
func SetLevel(l zapcore.Level) {
lvl.SetLevel(l)
}
// S is a global logger.
func S() *zap.SugaredLogger {
return s
}
// Nop is a logger that never writes out logs.
func Nop() *zap.Logger {
return nop
}