v1.0.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2017 License: MIT Imports: 19 Imported by: 0



logs is a Go logs manager. It can use many logs adapters. The repo is inspired by database/sql .

How to install?

go get github.com/astaxie/beego/logs

What adapters are supported?

As of now this logs support console, file,smtp and conn.

How to use it?

First you must import it

import (

Then init a Log (example with console adapter)

log := NewLogger(10000)
log.SetLogger("console", "")	

the first params stand for how many channel

Use it like this:


File adapter

Configure file adapter like this:

log := NewLogger(10000)
log.SetLogger("file", `{"filename":"test.log"}`)

Conn adapter

Configure like this:

log := NewLogger(1000)
log.SetLogger("conn", `{"net":"tcp","addr":":7020"}`)

Smtp adapter

Configure like this:

log := NewLogger(10000)
log.SetLogger("smtp", `{"username":"beegotest@gmail.com","password":"xxxxxxxx","host":"smtp.gmail.com:587","sendTos":["xiemengjun@gmail.com"]}`)
log.Critical("sendmail critical")
time.Sleep(time.Second * 30)



Package logs provide a general log interface Usage:

import "github.com/astaxie/beego/logs"

log := NewLogger(10000)
log.AddAdapter("console", "")

> the first params stand for how many channel

Use it like this:


 more docs http://beego.me/docs/module/logs.md



View Source
const (
	LevelSystem = iota

log message levels.


View Source
var Prefix = map[int]string{
	LevelSystem:        "",
	LevelFatal:         "[F]",
	LevelEmergency:     "[M]",
	LevelAlert:         "[A]",
	LevelCritical:      "[C]",
	LevelError:         "[E]",
	LevelWarning:       "[W]",
	LevelNotice:        "[N]",
	LevelInformational: "[I]",
	LevelDebug:         "[D]",


func Bytes2String added in v0.7.0

func Bytes2String(b []byte) string

Bytes2String直接转换底层指针,两者指向的相同的内存,改一个另外一个也会变。 效率是string([]byte{})的百倍以上,且转换量越大效率优势越明显。

func Register

func Register(name string, log loggerType)

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 String2Bytes added in v0.7.0

func String2Bytes(s string) []byte

String2Bytes直接转换底层指针,两者指向的相同的内存,改一个另外一个也会变。 效率是string([]byte{})的百倍以上,且转换量越大效率优势越明显。 转换之后若没做其他操作直接改变里面的字符,则程序会崩溃。 如 b:=String2bytes("xxx"); b[1]='d'; 程序将panic。


type BeeLogger

type BeeLogger struct {
	// contains filtered or unexported fields

BeeLogger is default logger in beego application. it can contain several providers and log message into all providers.

func NewLogger

func NewLogger(channelLen int64) *BeeLogger

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 (*BeeLogger) AddAdapter

func (bl *BeeLogger) AddAdapter(adapterName string, config string) error

AddAdapter provides a given logger adapter into BeeLogger with config string. config need to be correct JSON as string: {"interval":360}.

func (*BeeLogger) Alert

func (bl *BeeLogger) Alert(format string, v ...interface{})

Alert Log ALERT level message.

func (*BeeLogger) Close

func (bl *BeeLogger) Close()

Close close logger, flush all chan data and destroy all adapters in BeeLogger.

func (*BeeLogger) Critical

func (bl *BeeLogger) Critical(format string, v ...interface{})

Critical Log CRITICAL level message.

func (*BeeLogger) Debug

func (bl *BeeLogger) Debug(format string, v ...interface{})

Debug Log DEBUG level message.

func (*BeeLogger) DelAdapter

func (bl *BeeLogger) DelAdapter(adapterName string) error

DelLogger remove a logger adapter in BeeLogger.

func (*BeeLogger) Emergency

func (bl *BeeLogger) Emergency(format string, v ...interface{})

Emergency Log EMERGENCY level message.

func (*BeeLogger) EnableFuncCallDepth

func (bl *BeeLogger) EnableFuncCallDepth(b bool)

EnableFuncCallDepth enable log funcCallDepth

func (*BeeLogger) Error

func (bl *BeeLogger) Error(format string, v ...interface{})

Error Log ERROR level message.

func (*BeeLogger) Fatal

func (bl *BeeLogger) Fatal(format string, v ...interface{})

func (*BeeLogger) Flush

func (bl *BeeLogger) Flush()

Flush flush all chan data.

func (*BeeLogger) GetLogFuncCallDepth

func (bl *BeeLogger) GetLogFuncCallDepth() int

GetLogFuncCallDepth return log funcCallDepth for wrapper

func (*BeeLogger) Info

func (bl *BeeLogger) Info(format string, v ...interface{})

Info Log INFO level message. compatibility alias for Informational()

func (*BeeLogger) Notice

func (bl *BeeLogger) Notice(format string, v ...interface{})

Notice Log NOTICE level message.

func (*BeeLogger) Reset

func (bl *BeeLogger) Reset()

Reset close all outputs, and set bl.outputs to nil

func (*BeeLogger) SetLevel

func (bl *BeeLogger) SetLevel(l int)

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 (*BeeLogger) SetLogFuncCallDepth

func (bl *BeeLogger) SetLogFuncCallDepth(d int)

SetLogFuncCallDepth set log funcCallDepth

func (*BeeLogger) SetMsgChan

func (bl *BeeLogger) SetMsgChan(channelLen int64)

func (*BeeLogger) Sys

func (bl *BeeLogger) Sys(format string, v ...interface{})

func (*BeeLogger) Warn

func (bl *BeeLogger) Warn(format string, v ...interface{})

Warn Log WARN level message. compatibility alias for Warning()

func (*BeeLogger) Write

func (bl *BeeLogger) Write(p []byte) (n int, err error)


type Logger

type Logger interface {
	Init(config string) error
	WriteMsg(logMsg) error

Logger defines the behavior of a log provider.

func NewConn

func NewConn() Logger

NewConn create new ConnWrite returning as LoggerInterface.

func NewConsole

func NewConsole() Logger

NewConsole create ConsoleWriter returning as LoggerInterface.

type SMTPWriter

type SMTPWriter struct {
	Username           string   `json:"username"`
	Password           string   `json:"password"`
	Host               string   `json:"host"`
	Subject            string   `json:"subject"`
	FromAddress        string   `json:"fromAddress"`
	RecipientAddresses []string `json:"sendTos"`
	Level              int      `json:"level"`

SMTPWriter implements LoggerInterface and is used to send emails via given SMTP-server.

func (*SMTPWriter) Destroy

func (s *SMTPWriter) Destroy()

Destroy implementing method. empty.

func (*SMTPWriter) Flush

func (s *SMTPWriter) Flush()

Flush implementing method. empty.

func (*SMTPWriter) Init

func (s *SMTPWriter) Init(jsonconfig string) error

Init smtp writer with json config. config like:

	"subject":"email title",

func (*SMTPWriter) WriteMsg

func (s *SMTPWriter) WriteMsg(lm logMsg) error

WriteMsg write message in smtp writer. it will send an email with subject and only this message.


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL