Documentation
¶
Overview ¶
Package logit provides an easy way to use foundation for your logging operations.
1. The basic usage:
// Log messages with four levels. // Notice that the default level is info, so first line of debug message // will not be logged! If you want to change level, see logit.ChangeLevelTo logit.Debug("I am a debug message! But I will not be logged in default level!") logit.Info("I am an info message!") logit.Warn("I am a warn message!") logit.Error("I am an error message!") // Also, you can create a new independent Logger to use. See logit.NewLogger. // If you want format your message, just add arguments! logit.Info("format info message! id = %d, content = %s", 1, "info!")
2. logger:
// NewStdoutLogger creates a new Logger holder to standard output, generally a terminal or a console. logger := logit.NewStdoutLogger(logit.DebugLevel) // Then you will be easy to log! logger.Debug("this is a debug message!") logger.Info("this is a info message!") logger.Warn("this is a warn message!") logger.Error("this is a error message!") // NewLogger creates a new Logger holder. // The first parameter "os.Stdout" is a writer for logging. // As you know, file also can be written, just replace "os.Stdout" with your file! // The second parameter "logit.DebugLevel" is the level of this Logger. logger = logit.NewLogger(os.Stdout, logit.DebugLevel) // If you want format your message, just add arguments! logger.Info("format info message! id = %d, content = %s", 1, "info!")
3. enable or disable:
// Every new Logger is running. logger := logit.NewLogger(os.Stdout, logit.DebugLevel) logger.Info("I am running!") // Shutdown the Logger. // So the info message next line will not be logged! logger.Disable() logger.Info("I will not be logged!") // Enable the Logger. // The info message next line will be logged again! logger.Enable() logger.Info("I am running again!")
4. change logger level:
logit.Debug("Default logger level is info, so debug message will not be logged!") // Change logger level to debug level. logit.ChangeLevelTo(logit.DebugLevel) logit.Debug("Now debug message will be logged!")
5. log to file:
// NewFileLogger creates a new logger which logs to file. // It just need a file path like "D:/test.log" and a logger level. logger := logit.NewFileLogger("D:/test.log", logit.DebugLevel) logger.Info("我是 info 日志!") // NewDurationRollingLogger creates a duration rolling logger with given duration. // You should appoint a directory to store all log files generated in this time. // Notice that duration must not less than minDuration (generally time.Second), see wrapper.minDuration. // Also, default filename of log file is like "20200304-145246-45.log", see wrapper.NewFilename. // If you want to appoint another filename, check this and do it by this way. // See wrapper.NewDurationRollingFile (it is an implement of io.writer). logger = logit.NewDurationRollingLogger("D:/", time.Second, logit.DebugLevel) logger.Info("Rolling!!!")
Index ¶
- Constants
- func ChangeLevelTo(level LoggerLevel)
- func Debug(msg string, args ...interface{})
- func Disable()
- func Enable()
- func Error(msg string, args ...interface{})
- func Info(msg string, args ...interface{})
- func Warn(msg string, args ...interface{})
- type Logger
- func (l *Logger) ChangeLevelTo(newLevel LoggerLevel)
- func (l *Logger) Debug(msg string, args ...interface{})
- func (l *Logger) Disable()
- func (l *Logger) Enable()
- func (l *Logger) Error(msg string, args ...interface{})
- func (l *Logger) Info(msg string, args ...interface{})
- func (l *Logger) Warn(msg string, args ...interface{})
- type LoggerLevel
Constants ¶
const Version = "0.0.5"
Version is the version string representation of the "logit" package.
Variables ¶
This section is empty.
Functions ¶
func ChangeLevelTo ¶ added in v0.0.2
func ChangeLevelTo(level LoggerLevel)
ChangeLevelTo will change the level of logit to newLevel.
func Debug ¶ added in v0.0.2
func Debug(msg string, args ...interface{})
Debug will output msg as a debug message.
func Error ¶ added in v0.0.2
func Error(msg string, args ...interface{})
Error will output msg as an error message.
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a struct based on "log.Logger".
func NewDurationRollingLogger ¶ added in v0.0.5
func NewDurationRollingLogger(directory string, duration time.Duration, level LoggerLevel) *Logger
NewDurationRollingLogger creates a duration rolling logger with given duration. You should appoint a directory to store all log files generated in this time. Notice that duration must not less than minDuration (generally time.Second), see wrapper.minDuration. Also, default filename of log file is like "20200304-145246-45.log", see wrapper.NewFilename. If you want to appoint another filename, check this and do it by this way. See wrapper.NewDurationRollingFile (it is an implement of io.writer).
func NewFileLogger ¶ added in v0.0.5
func NewFileLogger(logFile string, level LoggerLevel) *Logger
NewFileLogger returns a Logger holder which log to a file with given logFile and level.
func NewLogger ¶
func NewLogger(out io.Writer, level LoggerLevel) *Logger
NewLogger create one Logger with given out and level. The first parameter out is a writer for logging. The second parameter level is the level of this Logger. It returns a new running Logger holder.
func NewStdoutLogger ¶ added in v0.0.2
func NewStdoutLogger(level LoggerLevel) *Logger
NewStdoutLogger returns a Logger holder with given logger level.
func (*Logger) ChangeLevelTo ¶ added in v0.0.2
func (l *Logger) ChangeLevelTo(newLevel LoggerLevel)
ChangeLevelTo will change the level of current Logger to newLevel.
type LoggerLevel ¶ added in v0.0.4
type LoggerLevel uint8
LoggerLevel is the type representation of the level.
const ( DebugLevel LoggerLevel = iota InfoLevel WarnLevel ErrorLevel )
Constants about logger level.