parent
414a65fe3f
commit
26138e128f
@ -1,3 +1,10 @@ |
||||
module go.bitcubix.io/log |
||||
|
||||
go 1.18 |
||||
|
||||
require ( |
||||
github.com/mattn/go-colorable v0.1.12 // indirect |
||||
github.com/mattn/go-isatty v0.0.14 // indirect |
||||
github.com/rs/zerolog v1.27.0 // indirect |
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect |
||||
) |
||||
|
@ -0,0 +1,13 @@ |
||||
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= |
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= |
||||
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= |
||||
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= |
||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= |
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= |
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= |
||||
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= |
||||
github.com/rs/zerolog v1.27.0 h1:1T7qCieN22GVc8S4Q2yuexzBb1EqjbgjSH9RohbMjKs= |
||||
github.com/rs/zerolog v1.27.0/go.mod h1:7frBqO0oezxmnO7GF86FY++uy8I0Tk/If5ni1G9Qc0U= |
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo= |
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
@ -0,0 +1,135 @@ |
||||
package log |
||||
|
||||
import ( |
||||
"context" |
||||
"fmt" |
||||
"io" |
||||
"os" |
||||
"path/filepath" |
||||
"time" |
||||
|
||||
"github.com/rs/zerolog" |
||||
) |
||||
|
||||
type Logger struct { |
||||
logger *zerolog.Logger |
||||
} |
||||
|
||||
func New(isDebug bool, logFile string) (*Logger, error) { |
||||
//zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
|
||||
logLevel := zerolog.InfoLevel |
||||
if isDebug { |
||||
logLevel = zerolog.DebugLevel |
||||
} |
||||
zerolog.SetGlobalLevel(logLevel) |
||||
|
||||
err := os.MkdirAll(filepath.Dir(logFile), os.ModePerm) |
||||
if err != nil { |
||||
fmt.Printf("error creating log directory: %v", err) |
||||
return nil, err |
||||
} |
||||
|
||||
f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm) |
||||
if err != nil { |
||||
fmt.Printf("error opening logfile: %v", err) |
||||
return nil, err |
||||
} |
||||
|
||||
multiOut := zerolog.MultiLevelWriter(f, zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC822Z}) |
||||
logger := zerolog.New(multiOut).With().Caller().Timestamp().Logger() |
||||
|
||||
// pretty print
|
||||
return &Logger{logger: &logger}, nil |
||||
} |
||||
|
||||
// Output duplicates the global logger and sets w as its output.
|
||||
func (l *Logger) Output(w io.Writer) zerolog.Logger { |
||||
return l.logger.Output(w) |
||||
} |
||||
|
||||
// With creates a child logger with the field added to its context.
|
||||
func (l *Logger) With() zerolog.Context { |
||||
return l.logger.With() |
||||
} |
||||
|
||||
// Level creates a child logger with the minimum accepted level set to level.
|
||||
func (l *Logger) Level(level zerolog.Level) zerolog.Logger { |
||||
return l.logger.Level(level) |
||||
} |
||||
|
||||
// Sample returns a logger with the s sampler.
|
||||
func (l *Logger) Sample(s zerolog.Sampler) zerolog.Logger { |
||||
return l.logger.Sample(s) |
||||
} |
||||
|
||||
// Hook returns a logger with the h Hook.
|
||||
func (l *Logger) Hook(h zerolog.Hook) zerolog.Logger { |
||||
return l.logger.Hook(h) |
||||
} |
||||
|
||||
// Debug starts a new message with debug level.
|
||||
// You must call Msg on the returned event in order to send the event.
|
||||
func (l *Logger) Debug() *zerolog.Event { |
||||
return l.logger.Debug() |
||||
} |
||||
|
||||
// Info starts a new message with info level.
|
||||
// You must call Msg on the returned event in order to send the event.
|
||||
func (l *Logger) Info() *zerolog.Event { |
||||
return l.logger.Info() |
||||
} |
||||
|
||||
// Warn starts a new message with warn level.
|
||||
// You must call Msg on the returned event in order to send the event.
|
||||
func (l *Logger) Warn() *zerolog.Event { |
||||
return l.logger.Warn() |
||||
} |
||||
|
||||
// Error starts a new message with error level.
|
||||
// You must call Msg on the returned event in order to send the event.
|
||||
func (l *Logger) Error() *zerolog.Event { |
||||
return l.logger.Error() |
||||
} |
||||
|
||||
// Fatal starts a new message with fatal level. The os.Exit(1) function is called by the Msg method.
|
||||
// You must call Msg on the returned event in order to send the event.
|
||||
func (l *Logger) Fatal() *zerolog.Event { |
||||
return l.logger.Fatal() |
||||
} |
||||
|
||||
// Panic starts a new message with panic level. The message is also sent to the panic function.
|
||||
// You must call Msg on the returned event in order to send the event.
|
||||
func (l *Logger) Panic() *zerolog.Event { |
||||
return l.logger.Panic() |
||||
} |
||||
|
||||
// WithLevel starts a new message with level.
|
||||
// You must call Msg on the returned event in order to send the event.
|
||||
func (l *Logger) WithLevel(level zerolog.Level) *zerolog.Event { |
||||
return l.logger.WithLevel(level) |
||||
} |
||||
|
||||
// Log starts a new message with no level. Setting zerolog.GlobalLevel to
|
||||
// zerolog.Disabled will still disable events produced by this method.
|
||||
//
|
||||
// You must call Msg on the returned event in order to send the event.
|
||||
func (l *Logger) Log() *zerolog.Event { |
||||
return l.logger.Log() |
||||
} |
||||
|
||||
// Print sends a log event using debug level and no extra field.
|
||||
// Arguments are handled in the manner of fmt.Print.
|
||||
func (l *Logger) Print(v ...interface{}) { |
||||
l.logger.Print(v...) |
||||
} |
||||
|
||||
// Printf sends a log event using debug level and no extra field.
|
||||
// Arguments are handled in the manner of fmt.Printf.
|
||||
func (l *Logger) Printf(format string, v ...interface{}) { |
||||
l.logger.Printf(format, v...) |
||||
} |
||||
|
||||
// Ctx returns the Logger associated with the ctx. If no logger is associated, a disabled logger is returned.
|
||||
func (l *Logger) Ctx(ctx context.Context) *Logger { |
||||
return &Logger{logger: zerolog.Ctx(ctx)} |
||||
} |
Loading…
Reference in new issue