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!") // If you want to output log with file info, try this: logit.EnableFileInfo() logit.Info("Show file 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!") // If you want format your time, try this: logger.SetFormatOfTime("2006/01/02 15:04:05") logger.Info("What time is it now?") // If you want to output log with file info, try this: logger.EnableFileInfo() logger.Info("What file is it? Which line?") logger.DisableFileInfo()
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!!!") // NewDayRollingLogger creates a day rolling logger. // You should appoint a directory to store all log files generated in this time. // See logit.NewDurationRollingLogger. logger = logit.NewDayRollingLogger("D:/", logit.DebugLevel) logger.Info("Today is Friday!!!") // NewSizeRollingLogger creates a file size rolling logger with given limitedSize. // You should appoint a directory to store all log files generated in this time. // Notice that limitedSize must not less than minLimitedSize (generally 64 KB), see wrapper.minLimitedSize. // Check wrapper.KB, wrapper.MB, wrapper.GB to know what unit you gonna to use. // Also, default filename of log file is like "20200304-145246-45.log", see nextFilename. // If you want to appoint another filename, check this and do it by this way. // See wrapper.NewSizeRollingFile (it is an implement of io.writer). logger = logit.NewSizeRollingLogger("D:/", 64*wrapper.KB, logit.DebugLevel) logger.Info("file size???") // NewDayRollingLogger creates a file size rolling logger. // You should appoint a directory to store all log files generated in this time. // Default means limitedSize is 64 MB. See NewSizeRollingLogger. logger = logit.NewDefaultSizeRollingLogger("D:/", logit.DebugLevel) logger.Info("64 MB rolling!!!")
6. logger handler:
// Create a logger holder. // Default handler is logit.DefaultLoggerHandler. logger := logit.NewLogger(os.Stdout, logit.InfoLevel) logger.Info("before logging...") // Customize your own handler. handlers1 := func(logger *logit.Logger, level logit.LoggerLevel, now time.Time, msg string) bool { logger.Writer().Write([]byte("handlers1: " + msg + "\n")) return true } handlers2 := func(logger *logit.Logger, level logit.LoggerLevel, now time.Time, msg string) bool { logger.Writer().Write([]byte("handlers2: " + msg + "\n")) return true } // Add handlers to logger. // There are three handlers in logger because logger has a default handler inside after creating. // See logit.DefaultLoggerHandler. logger.AddHandlers(handlers1, handlers2) fmt.Println("fmt =========================================") logger.Info("after adding handlers...") // Set handlers to logger. // There are two handlers in logger because the default handler inside was removed. logger.SetHandlers(handlers1, handlers2) fmt.Println("fmt =========================================") logger.Info("after setting handlers...")
Index ¶
- Constants
- func AddHandlers(handlers ...LoggerHandler)
- func ChangeLevelTo(level LoggerLevel)
- func Debug(msg string, args ...interface{})
- func DefaultLoggerHandler(logger *Logger, level LoggerLevel, now time.Time, msg string) bool
- func Disable()
- func DisableFileInfo()
- func Enable()
- func EnableFileInfo()
- func Error(msg string, args ...interface{})
- func Info(msg string, args ...interface{})
- func SetFormatOfTime(formatOfTime string)
- func SetHandlers(handlers ...LoggerHandler) bool
- func Warn(msg string, args ...interface{})
- type Logger
- func NewDayRollingLogger(directory string, level LoggerLevel) *Logger
- func NewDefaultSizeRollingLogger(directory string, level LoggerLevel) *Logger
- func NewDurationRollingLogger(directory string, duration time.Duration, level LoggerLevel) *Logger
- func NewFileLogger(logFile string, level LoggerLevel) *Logger
- func NewLogger(writer io.Writer, level LoggerLevel) *Logger
- func NewLoggerWithHandlers(writer io.Writer, level LoggerLevel, handlers ...LoggerHandler) *Logger
- func NewSizeRollingLogger(directory string, limitedSize int64, level LoggerLevel) *Logger
- func NewStdoutLogger(level LoggerLevel) *Logger
- func (l *Logger) AddHandlers(handlers ...LoggerHandler)
- func (l *Logger) ChangeLevelTo(newLevel LoggerLevel)
- func (l *Logger) Debug(msg string, args ...interface{})
- func (l *Logger) Disable()
- func (l *Logger) DisableFileInfo()
- func (l *Logger) Enable()
- func (l *Logger) EnableFileInfo()
- func (l *Logger) Error(msg string, args ...interface{})
- func (l *Logger) Info(msg string, args ...interface{})
- func (l *Logger) SetFormatOfTime(formatOfTime string)
- func (l *Logger) SetHandlers(handlers ...LoggerHandler) bool
- func (l *Logger) Warn(msg string, args ...interface{})
- func (l *Logger) Writer() io.Writer
- type LoggerHandler
- type LoggerLevel
Constants ¶
const DefaultFormatOfTime = "2006-01-02 15:04:05"
DefaultFormatOfTime is the default format for formatting time.
const PrefixOfLogFile = ".log"
PrefixOfLogFile is the prefix of log file.
const Version = "0.0.7"
Version is the version string representation of the "logit" package.
Variables ¶
This section is empty.
Functions ¶
func AddHandlers ¶ added in v0.0.7
func AddHandlers(handlers ...LoggerHandler)
AddHandlers adds more handlers to logit, and all handlers added before will be retained. If you want to remove all handlers, try logit.SetHandlers(). See logit.DefaultLoggerHandler.
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 DefaultLoggerHandler ¶ added in v0.0.7
DefaultLoggerHandler is the default handler in logit. The log handled by this handler will be like "Info [2020-03-06 16:10:44] msg". If you want to customize, just code your own handler, then replace it!
func DisableFileInfo ¶ added in v0.0.7
func DisableFileInfo()
DisableFileInfo means every log will not contain file info like line number. If you want file info again, try logit.EnableFileInfo().
func EnableFileInfo ¶ added in v0.0.7
func EnableFileInfo()
EnableFileInfo means every log will contain file info like line number. However, you should know that this is expensive in time. So be sure you really need it or keep it disabled.
func Error ¶ added in v0.0.2
func Error(msg string, args ...interface{})
Error will output msg as an error message.
func Info ¶ added in v0.0.2
func Info(msg string, args ...interface{})
Info will output msg as an info message.
func SetFormatOfTime ¶ added in v0.0.7
func SetFormatOfTime(formatOfTime string)
SetFormatOfTime sets format of time as you want. Default is "2006-01-02 15:04:05", see logit.DefaultFormatOfTime.
func SetHandlers ¶ added in v0.0.7
func SetHandlers(handlers ...LoggerHandler) bool
SetHandlers replaces logit's handlers with handlers, all handlers added before will be removed. If you want to add more handlers rather than replace them, try logit.AddHandlers(). Notice that at least one handler should be added, so if len(handlers) < 1, it returns false which means setting failed. Return true if setting is successful. See logit.DefaultLoggerHandler.
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a struct to log.
func NewDayRollingLogger ¶ added in v0.0.6
func NewDayRollingLogger(directory string, level LoggerLevel) *Logger
NewDayRollingLogger creates a day rolling logger. You should appoint a directory to store all log files generated in this time. See NewDurationRollingLogger.
func NewDefaultSizeRollingLogger ¶ added in v0.0.6
func NewDefaultSizeRollingLogger(directory string, level LoggerLevel) *Logger
NewDayRollingLogger creates a file size rolling logger. You should appoint a directory to store all log files generated in this time. Default means limitedSize is 64 MB. See NewSizeRollingLogger.
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 one second), see wrapper.minDuration. Also, default filename of log file is like "20200304-145246-45.log", see nextFilename. 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(writer io.Writer, level LoggerLevel) *Logger
NewLogger creates one Logger with given out and level. The first parameter writer is the writer for logging. The second parameter level is the level of this Logger. It returns a new running Logger holder.
func NewLoggerWithHandlers ¶ added in v0.0.7
func NewLoggerWithHandlers(writer io.Writer, level LoggerLevel, handlers ...LoggerHandler) *Logger
NewLoggerWithHandlers creates one Logger with given out and level and handlers. The first parameter writer is the writer for logging. The second parameter level is the level of this Logger. The third parameter handlers is all logger handlers for handling each log. It returns a new running Logger holder.
func NewSizeRollingLogger ¶ added in v0.0.6
func NewSizeRollingLogger(directory string, limitedSize int64, level LoggerLevel) *Logger
NewSizeRollingLogger creates a file size rolling logger with given limitedSize. You should appoint a directory to store all log files generated in this time. Notice that limitedSize must not less than minLimitedSize (generally 64 KB), see wrapper.minLimitedSize. Check wrapper.KB, wrapper.MB, wrapper.GB to know what unit you gonna to use. Also, default filename of log file is like "20200304-145246-45.log", see nextFilename. If you want to appoint another filename, check this and do it by this way. See wrapper.NewSizeRollingFile (it is an implement of io.writer).
func NewStdoutLogger ¶ added in v0.0.2
func NewStdoutLogger(level LoggerLevel) *Logger
NewStdoutLogger returns a Logger holder with given logger level.
func (*Logger) AddHandlers ¶ added in v0.0.7
func (l *Logger) AddHandlers(handlers ...LoggerHandler)
AddHandlers adds more handlers to l, and all handlers added before will be retained. If you want to remove all handlers, try l.SetHandlers(). See logit.DefaultLoggerHandler.
func (*Logger) ChangeLevelTo ¶ added in v0.0.2
func (l *Logger) ChangeLevelTo(newLevel LoggerLevel)
ChangeLevelTo will change the level of current Logger to newLevel.
func (*Logger) DisableFileInfo ¶ added in v0.0.7
func (l *Logger) DisableFileInfo()
DisableFileInfo means every log will not contain file info like line number. If you want file info again, try l.EnableFileInfo().
func (*Logger) EnableFileInfo ¶ added in v0.0.7
func (l *Logger) EnableFileInfo()
EnableFileInfo means every log will contain file info like line number. However, you should know that this is expensive in time. So be sure you really need it or keep it disabled.
func (*Logger) SetFormatOfTime ¶ added in v0.0.7
SetFormatOfTime sets format of time as you want. Default is "2006-01-02 15:04:05", see DefaultFormatOfTime.
func (*Logger) SetHandlers ¶ added in v0.0.7
func (l *Logger) SetHandlers(handlers ...LoggerHandler) bool
SetHandlers replaces l.handlers with handlers, all handlers added before will be removed. If you want to add more handlers rather than replace them, try l.AddHandlers. Notice that at least one handler should be added, so if len(handlers) < 1, it returns false which means setting failed. Return true if setting is successful. See logit.DefaultLoggerHandler.
type LoggerHandler ¶ added in v0.0.7
LoggerHandler is a struct representation of log handler. Every log will handle by this handler, and you can customize your own handler to handle logs in your way. The return value is meaningful, false means next handler will not be handled, only true will go on handling process. Notice that if one handler returns false, then all handlers after it will not use anymore.
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.