2014-02-24 04:50:42 +04:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"sync"
|
2017-03-23 19:13:19 +03:00
|
|
|
"sync/atomic"
|
2018-06-29 17:53:51 +03:00
|
|
|
"time"
|
2014-02-24 04:50:42 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Logger struct {
|
2014-03-12 18:34:29 +04:00
|
|
|
// The logs are `io.Copy`'d to this in a mutex. It's common to set this to a
|
2015-08-07 11:56:08 +03:00
|
|
|
// file, or leave it default which is `os.Stderr`. You can also set this to
|
2017-10-09 18:18:43 +03:00
|
|
|
// something more adventurous, such as logging to Kafka.
|
2014-03-12 18:34:29 +04:00
|
|
|
Out io.Writer
|
|
|
|
// Hooks for the logger instance. These allow firing events based on logging
|
|
|
|
// levels and log entries. For example, to send errors to an error tracking
|
|
|
|
// service, log to StatsD or dump the core on fatal errors.
|
2015-06-26 18:42:49 +03:00
|
|
|
Hooks LevelHooks
|
2014-03-12 18:34:29 +04:00
|
|
|
// All log entries pass through the formatter before logged to Out. The
|
|
|
|
// included formatters are `TextFormatter` and `JSONFormatter` for which
|
|
|
|
// TextFormatter is the default. In development (when a TTY is attached) it
|
|
|
|
// logs with colors, but to a file it wouldn't. You can easily implement your
|
|
|
|
// own that implements the `Formatter` interface, see the `README` or included
|
|
|
|
// formatters for examples.
|
2014-03-11 03:22:08 +04:00
|
|
|
Formatter Formatter
|
2016-11-26 06:02:56 +03:00
|
|
|
|
2018-10-23 07:22:00 +03:00
|
|
|
// Flag for whether to log caller info (off by default)
|
2016-11-29 20:35:34 +03:00
|
|
|
ReportCaller bool
|
2016-11-26 06:02:56 +03:00
|
|
|
|
2014-03-12 18:34:29 +04:00
|
|
|
// The logging level the logger should log at. This is typically (and defaults
|
|
|
|
// to) `logrus.Info`, which allows Info(), Warn(), Error() and Fatal() to be
|
2017-07-19 18:47:53 +03:00
|
|
|
// logged.
|
2014-03-12 18:34:29 +04:00
|
|
|
Level Level
|
2016-06-18 22:21:15 +03:00
|
|
|
// Used to sync writing to the log. Locking is enabled by Default
|
|
|
|
mu MutexWrap
|
2016-06-18 23:08:25 +03:00
|
|
|
// Reusable empty entry
|
|
|
|
entryPool sync.Pool
|
2018-10-10 16:59:03 +03:00
|
|
|
// Function to exit the application, defaults to `os.Exit()`
|
2018-10-10 16:54:15 +03:00
|
|
|
ExitFunc exitFunc
|
2016-06-18 22:21:15 +03:00
|
|
|
}
|
|
|
|
|
2018-10-10 16:57:58 +03:00
|
|
|
type exitFunc func(int)
|
|
|
|
|
2016-06-18 22:21:15 +03:00
|
|
|
type MutexWrap struct {
|
|
|
|
lock sync.Mutex
|
|
|
|
disabled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw *MutexWrap) Lock() {
|
|
|
|
if !mw.disabled {
|
|
|
|
mw.lock.Lock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw *MutexWrap) Unlock() {
|
|
|
|
if !mw.disabled {
|
|
|
|
mw.lock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw *MutexWrap) Disable() {
|
|
|
|
mw.disabled = true
|
2014-03-12 18:34:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new logger. Configuration should be set by changing `Formatter`,
|
|
|
|
// `Out` and `Hooks` directly on the default logger instance. You can also just
|
|
|
|
// instantiate your own:
|
|
|
|
//
|
|
|
|
// var log = &Logger{
|
|
|
|
// Out: os.Stderr,
|
|
|
|
// Formatter: new(JSONFormatter),
|
2015-06-26 18:42:49 +03:00
|
|
|
// Hooks: make(LevelHooks),
|
2014-11-27 09:54:16 +03:00
|
|
|
// Level: logrus.DebugLevel,
|
2014-03-12 18:34:29 +04:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// It's recommended to make this a global instance called `log`.
|
2014-02-24 04:50:42 +04:00
|
|
|
func New() *Logger {
|
|
|
|
return &Logger{
|
2016-11-26 06:02:56 +03:00
|
|
|
Out: os.Stderr,
|
|
|
|
Formatter: new(TextFormatter),
|
|
|
|
Hooks: make(LevelHooks),
|
|
|
|
Level: InfoLevel,
|
2018-10-23 07:22:00 +03:00
|
|
|
ExitFunc: os.Exit,
|
2016-11-29 20:35:34 +03:00
|
|
|
ReportCaller: false,
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-18 23:08:25 +03:00
|
|
|
func (logger *Logger) newEntry() *Entry {
|
|
|
|
entry, ok := logger.entryPool.Get().(*Entry)
|
|
|
|
if ok {
|
|
|
|
return entry
|
|
|
|
}
|
|
|
|
return NewEntry(logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) releaseEntry(entry *Entry) {
|
2018-07-31 19:08:27 +03:00
|
|
|
entry.Data = map[string]interface{}{}
|
2016-06-18 23:08:25 +03:00
|
|
|
logger.entryPool.Put(entry)
|
|
|
|
}
|
|
|
|
|
2016-05-06 06:48:51 +03:00
|
|
|
// Adds a field to the log entry, note that it doesn't log until you call
|
2018-05-17 12:02:39 +03:00
|
|
|
// Debug, Print, Info, Warn, Error, Fatal or Panic. It only creates a log entry.
|
2015-08-20 07:07:28 +03:00
|
|
|
// If you want multiple fields, use `WithFields`.
|
2014-02-24 04:50:42 +04:00
|
|
|
func (logger *Logger) WithField(key string, value interface{}) *Entry {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
defer logger.releaseEntry(entry)
|
|
|
|
return entry.WithField(key, value)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
2014-03-12 18:34:29 +04:00
|
|
|
// Adds a struct of fields to the log entry. All it does is call `WithField` for
|
|
|
|
// each `Field`.
|
2014-02-24 04:50:42 +04:00
|
|
|
func (logger *Logger) WithFields(fields Fields) *Entry {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
defer logger.releaseEntry(entry)
|
|
|
|
return entry.WithFields(fields)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
2015-11-22 09:59:38 +03:00
|
|
|
// Add an error as single field to the log entry. All it does is call
|
|
|
|
// `WithError` for the given `error`.
|
|
|
|
func (logger *Logger) WithError(err error) *Entry {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
defer logger.releaseEntry(entry)
|
|
|
|
return entry.WithError(err)
|
2015-11-22 09:59:38 +03:00
|
|
|
}
|
|
|
|
|
2018-06-29 17:53:51 +03:00
|
|
|
// Overrides the time of the log entry.
|
|
|
|
func (logger *Logger) WithTime(t time.Time) *Entry {
|
|
|
|
entry := logger.newEntry()
|
|
|
|
defer logger.releaseEntry(entry)
|
|
|
|
return entry.WithTime(t)
|
|
|
|
}
|
|
|
|
|
2017-10-20 15:40:54 +03:00
|
|
|
func (logger *Logger) Tracef(format string, args ...interface{}) {
|
|
|
|
if logger.IsLevelEnabled(TraceLevel) {
|
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Tracef(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-24 04:50:42 +04:00
|
|
|
func (logger *Logger) Debugf(format string, args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(DebugLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Debugf(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Infof(format string, args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(InfoLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Infof(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Printf(format string, args ...interface{}) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Printf(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 03:22:08 +04:00
|
|
|
func (logger *Logger) Warnf(format string, args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(WarnLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Warnf(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-03-11 03:22:08 +04:00
|
|
|
}
|
|
|
|
|
2014-02-24 04:50:42 +04:00
|
|
|
func (logger *Logger) Warningf(format string, args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(WarnLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Warnf(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-03-11 03:22:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Errorf(format string, args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(ErrorLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Errorf(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Fatalf(format string, args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(FatalLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Fatalf(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2018-10-06 13:08:19 +03:00
|
|
|
logger.Exit(1)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Panicf(format string, args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(PanicLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Panicf(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
2017-10-20 15:40:54 +03:00
|
|
|
func (logger *Logger) Trace(args ...interface{}) {
|
|
|
|
if logger.IsLevelEnabled(TraceLevel) {
|
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Trace(args...)
|
|
|
|
logger.releaseEntry(entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-24 04:50:42 +04:00
|
|
|
func (logger *Logger) Debug(args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(DebugLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Debug(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Info(args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(InfoLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Info(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Print(args ...interface{}) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Info(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 03:22:08 +04:00
|
|
|
func (logger *Logger) Warn(args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(WarnLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Warn(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-03-11 03:22:08 +04:00
|
|
|
}
|
|
|
|
|
2014-02-24 04:50:42 +04:00
|
|
|
func (logger *Logger) Warning(args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(WarnLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Warn(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-03-11 03:22:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Error(args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(ErrorLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Error(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Fatal(args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(FatalLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Fatal(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2018-10-06 13:08:19 +03:00
|
|
|
logger.Exit(1)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Panic(args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(PanicLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Panic(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
2017-10-20 15:40:54 +03:00
|
|
|
func (logger *Logger) Traceln(args ...interface{}) {
|
|
|
|
if logger.IsLevelEnabled(TraceLevel) {
|
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Traceln(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Debugln(args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(DebugLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Debugln(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Infoln(args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(InfoLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Infoln(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Println(args ...interface{}) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Println(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 03:22:08 +04:00
|
|
|
func (logger *Logger) Warnln(args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(WarnLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Warnln(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-03-11 03:22:08 +04:00
|
|
|
}
|
|
|
|
|
2014-02-24 04:50:42 +04:00
|
|
|
func (logger *Logger) Warningln(args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(WarnLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Warnln(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-03-11 03:22:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Errorln(args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(ErrorLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Errorln(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Fatalln(args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(FatalLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Fatalln(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2018-10-06 13:08:19 +03:00
|
|
|
logger.Exit(1)
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Panicln(args ...interface{}) {
|
2018-08-27 00:51:09 +03:00
|
|
|
if logger.IsLevelEnabled(PanicLevel) {
|
2016-06-18 23:08:25 +03:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Panicln(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-19 14:21:56 +03:00
|
|
|
}
|
2014-02-24 04:50:42 +04:00
|
|
|
}
|
2016-06-18 22:21:15 +03:00
|
|
|
|
2018-10-10 16:54:15 +03:00
|
|
|
func (logger *Logger) Exit(code int) {
|
|
|
|
runHandlers()
|
|
|
|
if logger.ExitFunc == nil {
|
2018-10-10 16:57:58 +03:00
|
|
|
logger.ExitFunc = os.Exit
|
2018-10-10 16:54:15 +03:00
|
|
|
}
|
|
|
|
logger.ExitFunc(code)
|
|
|
|
}
|
|
|
|
|
2016-06-18 22:21:15 +03:00
|
|
|
//When file is opened with appending mode, it's safe to
|
|
|
|
//write concurrently to a file (within 4k message on Linux).
|
|
|
|
//In these cases user can choose to disable the lock.
|
|
|
|
func (logger *Logger) SetNoLock() {
|
|
|
|
logger.mu.Disable()
|
|
|
|
}
|
2017-03-23 19:13:19 +03:00
|
|
|
|
|
|
|
func (logger *Logger) level() Level {
|
|
|
|
return Level(atomic.LoadUint32((*uint32)(&logger.Level)))
|
|
|
|
}
|
|
|
|
|
2017-12-21 22:04:49 +03:00
|
|
|
// SetLevel sets the logger level.
|
2017-07-07 23:29:37 +03:00
|
|
|
func (logger *Logger) SetLevel(level Level) {
|
2017-03-23 19:13:19 +03:00
|
|
|
atomic.StoreUint32((*uint32)(&logger.Level), uint32(level))
|
|
|
|
}
|
2017-07-15 09:33:14 +03:00
|
|
|
|
2018-07-09 05:36:46 +03:00
|
|
|
|
2017-07-15 09:33:14 +03:00
|
|
|
func (logger *Logger) Lock() {
|
|
|
|
logger.mu.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Unlock() {
|
|
|
|
logger.mu.Unlock()
|
|
|
|
}
|
2018-07-09 05:36:46 +03:00
|
|
|
|
2018-06-27 20:29:28 +03:00
|
|
|
func (logger *Logger) SetOutput(out io.Writer) {
|
|
|
|
logger.mu.Lock()
|
|
|
|
defer logger.mu.Unlock()
|
|
|
|
logger.Out = out
|
|
|
|
}
|
|
|
|
|
2017-12-21 22:04:49 +03:00
|
|
|
// GetLevel returns the logger level.
|
|
|
|
func (logger *Logger) GetLevel() Level {
|
|
|
|
return logger.level()
|
2018-06-27 20:29:28 +03:00
|
|
|
}
|
|
|
|
|
2017-12-21 22:04:49 +03:00
|
|
|
// AddHook adds a hook to the logger hooks.
|
2017-08-04 18:24:22 +03:00
|
|
|
func (logger *Logger) AddHook(hook Hook) {
|
|
|
|
logger.mu.Lock()
|
|
|
|
defer logger.mu.Unlock()
|
|
|
|
logger.Hooks.Add(hook)
|
|
|
|
}
|
2018-05-30 02:47:39 +03:00
|
|
|
|
2018-08-27 00:51:09 +03:00
|
|
|
// IsLevelEnabled checks if the log level of the logger is greater than the level param
|
|
|
|
func (logger *Logger) IsLevelEnabled(level Level) bool {
|
|
|
|
return logger.level() >= level
|
2018-05-30 02:47:39 +03:00
|
|
|
}
|
|
|
|
|
2017-12-21 22:04:49 +03:00
|
|
|
// SetFormatter sets the logger formatter.
|
|
|
|
func (logger *Logger) SetFormatter(formatter Formatter) {
|
|
|
|
logger.mu.Lock()
|
|
|
|
defer logger.mu.Unlock()
|
|
|
|
logger.Formatter = formatter
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOutput sets the logger output.
|
|
|
|
func (logger *Logger) SetOutput(output io.Writer) {
|
|
|
|
logger.mu.Lock()
|
|
|
|
defer logger.mu.Unlock()
|
|
|
|
logger.Out = output
|
|
|
|
}
|
2018-08-26 21:44:53 +03:00
|
|
|
|
2018-10-27 16:19:06 +03:00
|
|
|
func (logger *Logger) SetReportCaller(reportCaller bool) {
|
|
|
|
logger.mu.Lock()
|
|
|
|
defer logger.mu.Unlock()
|
|
|
|
logger.ReportCaller = reportCaller
|
|
|
|
}
|
|
|
|
|
2018-08-26 14:40:51 +03:00
|
|
|
// ReplaceHooks replaces the logger hooks and returns the old ones
|
|
|
|
func (logger *Logger) ReplaceHooks(hooks LevelHooks) LevelHooks {
|
2018-08-22 12:10:05 +03:00
|
|
|
logger.mu.Lock()
|
2018-08-26 14:40:51 +03:00
|
|
|
oldHooks := logger.Hooks
|
2018-08-22 12:10:05 +03:00
|
|
|
logger.Hooks = hooks
|
|
|
|
logger.mu.Unlock()
|
2018-08-26 14:40:51 +03:00
|
|
|
return oldHooks
|
2018-05-30 02:47:39 +03:00
|
|
|
}
|