2018-11-14 10:18:03 +01:00
package log
2019-01-29 17:54:05 +01:00
import (
"bufio"
"io"
"runtime"
"github.com/sirupsen/logrus"
)
2018-11-14 10:18:03 +01:00
// Debug logs a message at level Debug on the standard logger.
2022-10-14 18:52:08 +02:00
// Deprecated: use log.FromContext(ctx).Debug(...) instead.
2018-11-14 10:18:03 +01:00
func Debug ( args ... interface { } ) {
mainLogger . Debug ( args ... )
}
// Debugf logs a message at level Debug on the standard logger.
2022-10-14 18:52:08 +02:00
// Deprecated: use log.FromContext(ctx).Debugf(...) instead.
2018-11-14 10:18:03 +01:00
func Debugf ( format string , args ... interface { } ) {
mainLogger . Debugf ( format , args ... )
}
// Info logs a message at level Info on the standard logger.
2022-10-14 18:52:08 +02:00
// Deprecated: use log.FromContext(ctx).Info(...) instead.
2018-11-14 10:18:03 +01:00
func Info ( args ... interface { } ) {
mainLogger . Info ( args ... )
}
// Infof logs a message at level Info on the standard logger.
2022-10-14 18:52:08 +02:00
// Deprecated: use log.FromContext(ctx).Infof(...) instead.
2018-11-14 10:18:03 +01:00
func Infof ( format string , args ... interface { } ) {
mainLogger . Infof ( format , args ... )
}
// Warn logs a message at level Warn on the standard logger.
2022-10-14 18:52:08 +02:00
// Deprecated: use log.FromContext(ctx).Warn(...) instead.
2018-11-14 10:18:03 +01:00
func Warn ( args ... interface { } ) {
mainLogger . Warn ( args ... )
}
// Warnf logs a message at level Warn on the standard logger.
2022-10-14 18:52:08 +02:00
// Deprecated: use log.FromContext(ctx).Warnf(...) instead.
2018-11-14 10:18:03 +01:00
func Warnf ( format string , args ... interface { } ) {
mainLogger . Warnf ( format , args ... )
}
// Error logs a message at level Error on the standard logger.
2022-10-14 18:52:08 +02:00
// Deprecated: use log.FromContext(ctx).Error(...) instead.
2018-11-14 10:18:03 +01:00
func Error ( args ... interface { } ) {
mainLogger . Error ( args ... )
}
// Errorf logs a message at level Error on the standard logger.
2022-10-14 18:52:08 +02:00
// Deprecated: use log.FromContext(ctx).Errorf(...) instead.
2018-11-14 10:18:03 +01:00
func Errorf ( format string , args ... interface { } ) {
mainLogger . Errorf ( format , args ... )
}
// Panic logs a message at level Panic on the standard logger.
2022-10-14 18:52:08 +02:00
// Deprecated: use log.FromContext(ctx).Panic(...) instead.
2018-11-14 10:18:03 +01:00
func Panic ( args ... interface { } ) {
mainLogger . Panic ( args ... )
}
// Fatal logs a message at level Fatal on the standard logger.
2022-10-14 18:52:08 +02:00
// Deprecated: use log.FromContext(ctx).Fatal(...) instead.
2018-11-14 10:18:03 +01:00
func Fatal ( args ... interface { } ) {
mainLogger . Fatal ( args ... )
}
// Fatalf logs a message at level Fatal on the standard logger.
2022-10-14 18:52:08 +02:00
// Deprecated: use log.FromContext(ctx).Fatalf(...) instead.
2018-11-14 10:18:03 +01:00
func Fatalf ( format string , args ... interface { } ) {
mainLogger . Fatalf ( format , args ... )
}
// AddHook adds a hook to the standard logger hooks.
func AddHook ( hook logrus . Hook ) {
logrus . AddHook ( hook )
}
2019-01-29 17:54:05 +01:00
// CustomWriterLevel logs writer for a specific level. (with a custom scanner buffer size.)
2020-05-11 12:06:07 +02:00
// adapted from github.com/Sirupsen/logrus/writer.go.
2019-01-29 17:54:05 +01:00
func CustomWriterLevel ( level logrus . Level , maxScanTokenSize int ) * io . PipeWriter {
reader , writer := io . Pipe ( )
var printFunc func ( args ... interface { } )
switch level {
case logrus . DebugLevel :
printFunc = Debug
case logrus . InfoLevel :
printFunc = Info
case logrus . WarnLevel :
printFunc = Warn
case logrus . ErrorLevel :
printFunc = Error
case logrus . FatalLevel :
printFunc = Fatal
case logrus . PanicLevel :
printFunc = Panic
default :
printFunc = mainLogger . Print
}
go writerScanner ( reader , maxScanTokenSize , printFunc )
runtime . SetFinalizer ( writer , writerFinalizer )
return writer
}
// extract from github.com/Sirupsen/logrus/writer.go
2020-05-11 12:06:07 +02:00
// Hack the buffer size.
2019-01-29 17:54:05 +01:00
func writerScanner ( reader io . ReadCloser , scanTokenSize int , printFunc func ( args ... interface { } ) ) {
scanner := bufio . NewScanner ( reader )
if scanTokenSize > bufio . MaxScanTokenSize {
buf := make ( [ ] byte , bufio . MaxScanTokenSize )
scanner . Buffer ( buf , scanTokenSize )
}
for scanner . Scan ( ) {
printFunc ( scanner . Text ( ) )
}
if err := scanner . Err ( ) ; err != nil {
Errorf ( "Error while reading from Writer: %s" , err )
}
reader . Close ( )
}
func writerFinalizer ( writer * io . PipeWriter ) {
writer . Close ( )
}