1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-15 08:00:54 +00:00
Beego/pkg/adapter/logs/log.go
2020-10-06 11:52:24 +08:00

348 lines
8.9 KiB
Go

// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package logs provide a general log interface
// Usage:
//
// import "github.com/astaxie/beego/logs"
//
// log := NewLogger(10000)
// log.SetLogger("console", "")
//
// > the first params stand for how many channel
//
// Use it like this:
//
// log.Trace("trace")
// log.Info("info")
// log.Warn("warning")
// log.Debug("debug")
// log.Critical("critical")
//
// more docs http://beego.me/docs/module/logs.md
package logs
import (
"log"
"time"
"github.com/astaxie/beego/pkg/core/logs"
)
// RFC5424 log message levels.
const (
LevelEmergency = iota
LevelAlert
LevelCritical
LevelError
LevelWarning
LevelNotice
LevelInformational
LevelDebug
)
// levelLogLogger is defined to implement log.Logger
// the real log level will be LevelEmergency
const levelLoggerImpl = -1
// Name for adapter with beego official support
const (
AdapterConsole = "console"
AdapterFile = "file"
AdapterMultiFile = "multifile"
AdapterMail = "smtp"
AdapterConn = "conn"
AdapterEs = "es"
AdapterJianLiao = "jianliao"
AdapterSlack = "slack"
AdapterAliLS = "alils"
)
// Legacy log level constants to ensure backwards compatibility.
const (
LevelInfo = LevelInformational
LevelTrace = LevelDebug
LevelWarn = LevelWarning
)
type newLoggerFunc func() Logger
// Logger defines the behavior of a log provider.
type Logger interface {
Init(config string) error
WriteMsg(when time.Time, msg string, level int) error
Destroy()
Flush()
}
// Register makes a log provide available by the provided name.
// If Register is called twice with the same name or if driver is nil,
// it panics.
func Register(name string, log newLoggerFunc) {
logs.Register(name, func() logs.Logger {
return &oldToNewAdapter{
old: log(),
}
})
}
// BeeLogger is default logger in beego application.
// it can contain several providers and log message into all providers.
type BeeLogger logs.BeeLogger
const defaultAsyncMsgLen = 1e3
// NewLogger returns a new BeeLogger.
// channelLen means the number of messages in chan(used where asynchronous is true).
// if the buffering chan is full, logger adapters write to file or other way.
func NewLogger(channelLens ...int64) *BeeLogger {
return (*BeeLogger)(logs.NewLogger(channelLens...))
}
// Async set the log to asynchronous and start the goroutine
func (bl *BeeLogger) Async(msgLen ...int64) *BeeLogger {
(*logs.BeeLogger)(bl).Async(msgLen...)
return bl
}
// SetLogger provides a given logger adapter into BeeLogger with config string.
// config need to be correct JSON as string: {"interval":360}.
func (bl *BeeLogger) SetLogger(adapterName string, configs ...string) error {
return (*logs.BeeLogger)(bl).SetLogger(adapterName, configs...)
}
// DelLogger remove a logger adapter in BeeLogger.
func (bl *BeeLogger) DelLogger(adapterName string) error {
return (*logs.BeeLogger)(bl).DelLogger(adapterName)
}
func (bl *BeeLogger) Write(p []byte) (n int, err error) {
return (*logs.BeeLogger)(bl).Write(p)
}
// SetLevel Set log message level.
// If message level (such as LevelDebug) is higher than logger level (such as LevelWarning),
// log providers will not even be sent the message.
func (bl *BeeLogger) SetLevel(l int) {
(*logs.BeeLogger)(bl).SetLevel(l)
}
// GetLevel Get Current log message level.
func (bl *BeeLogger) GetLevel() int {
return (*logs.BeeLogger)(bl).GetLevel()
}
// SetLogFuncCallDepth set log funcCallDepth
func (bl *BeeLogger) SetLogFuncCallDepth(d int) {
(*logs.BeeLogger)(bl).SetLogFuncCallDepth(d)
}
// GetLogFuncCallDepth return log funcCallDepth for wrapper
func (bl *BeeLogger) GetLogFuncCallDepth() int {
return (*logs.BeeLogger)(bl).GetLogFuncCallDepth()
}
// EnableFuncCallDepth enable log funcCallDepth
func (bl *BeeLogger) EnableFuncCallDepth(b bool) {
(*logs.BeeLogger)(bl).EnableFuncCallDepth(b)
}
// set prefix
func (bl *BeeLogger) SetPrefix(s string) {
(*logs.BeeLogger)(bl).SetPrefix(s)
}
// Emergency Log EMERGENCY level message.
func (bl *BeeLogger) Emergency(format string, v ...interface{}) {
(*logs.BeeLogger)(bl).Emergency(format, v...)
}
// Alert Log ALERT level message.
func (bl *BeeLogger) Alert(format string, v ...interface{}) {
(*logs.BeeLogger)(bl).Alert(format, v...)
}
// Critical Log CRITICAL level message.
func (bl *BeeLogger) Critical(format string, v ...interface{}) {
(*logs.BeeLogger)(bl).Critical(format, v...)
}
// Error Log ERROR level message.
func (bl *BeeLogger) Error(format string, v ...interface{}) {
(*logs.BeeLogger)(bl).Error(format, v...)
}
// Warning Log WARNING level message.
func (bl *BeeLogger) Warning(format string, v ...interface{}) {
(*logs.BeeLogger)(bl).Warning(format, v...)
}
// Notice Log NOTICE level message.
func (bl *BeeLogger) Notice(format string, v ...interface{}) {
(*logs.BeeLogger)(bl).Notice(format, v...)
}
// Informational Log INFORMATIONAL level message.
func (bl *BeeLogger) Informational(format string, v ...interface{}) {
(*logs.BeeLogger)(bl).Informational(format, v...)
}
// Debug Log DEBUG level message.
func (bl *BeeLogger) Debug(format string, v ...interface{}) {
(*logs.BeeLogger)(bl).Debug(format, v...)
}
// Warn Log WARN level message.
// compatibility alias for Warning()
func (bl *BeeLogger) Warn(format string, v ...interface{}) {
(*logs.BeeLogger)(bl).Warn(format, v...)
}
// Info Log INFO level message.
// compatibility alias for Informational()
func (bl *BeeLogger) Info(format string, v ...interface{}) {
(*logs.BeeLogger)(bl).Info(format, v...)
}
// Trace Log TRACE level message.
// compatibility alias for Debug()
func (bl *BeeLogger) Trace(format string, v ...interface{}) {
(*logs.BeeLogger)(bl).Trace(format, v...)
}
// Flush flush all chan data.
func (bl *BeeLogger) Flush() {
(*logs.BeeLogger)(bl).Flush()
}
// Close close logger, flush all chan data and destroy all adapters in BeeLogger.
func (bl *BeeLogger) Close() {
(*logs.BeeLogger)(bl).Close()
}
// Reset close all outputs, and set bl.outputs to nil
func (bl *BeeLogger) Reset() {
(*logs.BeeLogger)(bl).Reset()
}
// GetBeeLogger returns the default BeeLogger
func GetBeeLogger() *BeeLogger {
return (*BeeLogger)(logs.GetBeeLogger())
}
// GetLogger returns the default BeeLogger
func GetLogger(prefixes ...string) *log.Logger {
return logs.GetLogger(prefixes...)
}
// Reset will remove all the adapter
func Reset() {
logs.Reset()
}
// Async set the beelogger with Async mode and hold msglen messages
func Async(msgLen ...int64) *BeeLogger {
return (*BeeLogger)(logs.Async(msgLen...))
}
// SetLevel sets the global log level used by the simple logger.
func SetLevel(l int) {
logs.SetLevel(l)
}
// SetPrefix sets the prefix
func SetPrefix(s string) {
logs.SetPrefix(s)
}
// EnableFuncCallDepth enable log funcCallDepth
func EnableFuncCallDepth(b bool) {
logs.EnableFuncCallDepth(b)
}
// SetLogFuncCall set the CallDepth, default is 4
func SetLogFuncCall(b bool) {
logs.SetLogFuncCall(b)
}
// SetLogFuncCallDepth set log funcCallDepth
func SetLogFuncCallDepth(d int) {
logs.SetLogFuncCallDepth(d)
}
// SetLogger sets a new logger.
func SetLogger(adapter string, config ...string) error {
return logs.SetLogger(adapter, config...)
}
// Emergency logs a message at emergency level.
func Emergency(f interface{}, v ...interface{}) {
logs.Emergency(f, v...)
}
// Alert logs a message at alert level.
func Alert(f interface{}, v ...interface{}) {
logs.Alert(f, v...)
}
// Critical logs a message at critical level.
func Critical(f interface{}, v ...interface{}) {
logs.Critical(f, v...)
}
// Error logs a message at error level.
func Error(f interface{}, v ...interface{}) {
logs.Error(f, v...)
}
// Warning logs a message at warning level.
func Warning(f interface{}, v ...interface{}) {
logs.Warning(f, v...)
}
// Warn compatibility alias for Warning()
func Warn(f interface{}, v ...interface{}) {
logs.Warn(f, v...)
}
// Notice logs a message at notice level.
func Notice(f interface{}, v ...interface{}) {
logs.Notice(f, v...)
}
// Informational logs a message at info level.
func Informational(f interface{}, v ...interface{}) {
logs.Informational(f, v...)
}
// Info compatibility alias for Warning()
func Info(f interface{}, v ...interface{}) {
logs.Info(f, v...)
}
// Debug logs a message at debug level.
func Debug(f interface{}, v ...interface{}) {
logs.Debug(f, v...)
}
// Trace logs a message at trace level.
// compatibility alias for Warning()
func Trace(f interface{}, v ...interface{}) {
logs.Trace(f, v...)
}
func init() {
SetLogFuncCallDepth(4)
}