2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-11-10 11:31:58 +08:00
asynq/internal/log/log.go

112 lines
2.7 KiB
Go
Raw Normal View History

2020-03-09 22:11:16 +08:00
// Copyright 2020 Kentaro Hibino. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
// Package log exports logging related types and functions.
package log
import (
2020-05-06 13:10:11 +08:00
"fmt"
2020-03-09 22:11:16 +08:00
"io"
stdlog "log"
"os"
2020-03-09 22:11:16 +08:00
)
2020-05-06 13:10:11 +08:00
// Base supports logging with various log levels.
type Base interface {
// Debug logs a message at Debug level.
Debug(args ...interface{})
// Info logs a message at Info level.
Info(args ...interface{})
// Warn logs a message at Warning level.
Warn(args ...interface{})
// Error logs a message at Error level.
Error(args ...interface{})
// Fatal logs a message at Fatal level
// and process will exit with status set to 1.
Fatal(args ...interface{})
2020-03-09 22:11:16 +08:00
}
2020-05-06 13:10:11 +08:00
// baseLogger is a wrapper object around log.Logger from the standard library.
// It supports logging at various log levels.
2020-05-06 13:10:11 +08:00
type baseLogger struct {
2020-03-09 22:11:16 +08:00
*stdlog.Logger
}
// Debug logs a message at Debug level.
2020-05-06 13:10:11 +08:00
func (l *baseLogger) Debug(args ...interface{}) {
l.prefixPrint("DEBUG: ", args...)
}
// Info logs a message at Info level.
2020-05-06 13:10:11 +08:00
func (l *baseLogger) Info(args ...interface{}) {
l.prefixPrint("INFO: ", args...)
2020-03-09 22:11:16 +08:00
}
// Warn logs a message at Warning level.
2020-05-06 13:10:11 +08:00
func (l *baseLogger) Warn(args ...interface{}) {
l.prefixPrint("WARN: ", args...)
2020-03-09 22:11:16 +08:00
}
// Error logs a message at Error level.
2020-05-06 13:10:11 +08:00
func (l *baseLogger) Error(args ...interface{}) {
l.prefixPrint("ERROR: ", args...)
2020-03-09 22:11:16 +08:00
}
// Fatal logs a message at Fatal level
// and process will exit with status set to 1.
2020-05-06 13:10:11 +08:00
func (l *baseLogger) Fatal(args ...interface{}) {
l.prefixPrint("FATAL: ", args...)
os.Exit(1)
}
2020-05-06 13:10:11 +08:00
func (l *baseLogger) prefixPrint(prefix string, args ...interface{}) {
args = append([]interface{}{prefix}, args...)
l.Print(args...)
}
// newBase creates and returns a new instance of baseLogger.
func newBase(out io.Writer) *baseLogger {
prefix := fmt.Sprintf("asynq: pid=%d ", os.Getpid())
return &baseLogger{
stdlog.New(out, prefix, stdlog.Ldate|stdlog.Ltime|stdlog.Lmicroseconds|stdlog.LUTC),
}
}
// NewLogger creates and returns a new instance of Logger.
func NewLogger(base Base) *Logger {
if base == nil {
base = newBase(os.Stderr)
}
return &Logger{base}
}
// Logger logs message to io.Writer with various log levels.
type Logger struct {
Base
}
func (l *Logger) Debugf(format string, args ...interface{}) {
l.Debug(fmt.Sprintf(format, args...))
}
func (l *Logger) Infof(format string, args ...interface{}) {
l.Info(fmt.Sprintf(format, args...))
}
func (l *Logger) Warnf(format string, args ...interface{}) {
l.Warn(fmt.Sprintf(format, args...))
}
func (l *Logger) Errorf(format string, args ...interface{}) {
l.Error(fmt.Sprintf(format, args...))
}
func (l *Logger) Fatalf(format string, args ...interface{}) {
l.Fatal(fmt.Sprintf(format, args...))
}