logs

package
v0.0.0-...-861d2e8 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2016 License: BSD-2-Clause Imports: 14 Imported by: 1

README

logs

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

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 (
	"./libs/logs"
)

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:

log.Trace("trace")
log.Info("info")
log.Warn("warning")
log.Debug("debug")
log.Critical("critical")

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"}`)
log.Info("info")

Smtp adapter

Configure like this:

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

Documentation

Index

Constants

View Source
const (
	// log message levels
	LevelTrace = iota
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
	LevelCritical
)

Variables

This section is empty.

Functions

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.

Types

type AccessLogWriter

type AccessLogWriter struct {
	*log.Logger

	// The opened file
	Filename string `json:"filename"`

	// Rotate at size
	Maxsize int `json:"maxsize"`

	// Rotate daily
	Daily   bool  `json:"daily"`
	Maxdays int64 `json:"maxdays`

	Rotate    bool `json:"rotate"`
	MaxRotate int  `json:"maxrotate"`

	Level int `json:"level"`
	// contains filtered or unexported fields
}

AccessLogWriter implements LoggerInterface. It writes messages by lines limit, file size limit, or time frequency.

func (*AccessLogWriter) Destroy

func (w *AccessLogWriter) Destroy()

destroy file logger, close file writer.

func (*AccessLogWriter) DoRotate

func (w *AccessLogWriter) DoRotate() error

DoRotate means it need to write file in new file. new file name like xx.log.2013-01-01.2

func (*AccessLogWriter) Flush

func (w *AccessLogWriter) Flush()

flush file logger. there are no buffering messages in file logger in memory. flush file means sync file from disk.

func (*AccessLogWriter) Init

func (w *AccessLogWriter) Init(jsonconfig string) error

Init file logger with json config. jsonconfig like:

{
"filename":"logs/access.log",
"maxsize":1<<30,
"daily":true,
"maxdays":15,
"rotate":true
}

func (*AccessLogWriter) WriteMsg

func (w *AccessLogWriter) WriteMsg(msg string, level int) error

write logger message into file.

type AccessMuxWriter

type AccessMuxWriter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

an *os.File writer with locker.

func (*AccessMuxWriter) SetFd

func (l *AccessMuxWriter) SetFd(fd *os.File)

set os.File in writer.

func (*AccessMuxWriter) Write

func (l *AccessMuxWriter) Write(b []byte) (int, error)

write to os.File.

type Brush

type Brush func(string) string

func NewBrush

func NewBrush(color string) Brush

type ConnWriter

type ConnWriter struct {
	ReconnectOnMsg bool   `json:"reconnectOnMsg"`
	Reconnect      bool   `json:"reconnect"`
	Net            string `json:"net"`
	Addr           string `json:"addr"`
	Level          int    `json:"level"`
	// contains filtered or unexported fields
}

ConnWriter implements LoggerInterface. it writes messages in keep-live tcp connection.

func (*ConnWriter) Destroy

func (c *ConnWriter) Destroy()

destroy connection writer and close tcp listener.

func (*ConnWriter) Flush

func (c *ConnWriter) Flush()

implementing method. empty.

func (*ConnWriter) Init

func (c *ConnWriter) Init(jsonconfig string) error

init connection writer with json config. json config only need key "level".

func (*ConnWriter) WriteMsg

func (c *ConnWriter) WriteMsg(msg string, level int) error

write message in connection. if connection is down, try to re-connect.

type ConsoleWriter

type ConsoleWriter struct {
	Level int `json:"level"`
	// contains filtered or unexported fields
}

ConsoleWriter implements LoggerInterface and writes messages to terminal.

func (*ConsoleWriter) Destroy

func (c *ConsoleWriter) Destroy()

implementing method. empty.

func (*ConsoleWriter) Flush

func (c *ConsoleWriter) Flush()

implementing method. empty.

func (*ConsoleWriter) Init

func (c *ConsoleWriter) Init(jsonconfig string) error

init console logger. jsonconfig like '{"level":LevelTrace}'.

func (*ConsoleWriter) WriteMsg

func (c *ConsoleWriter) WriteMsg(msg string, level int) error

write message in console.

type FileLogWriter

type FileLogWriter struct {
	*log.Logger

	// The opened file
	Filename string `json:"filename"`

	// Rotate at size
	Maxsize int `json:"maxsize"`

	// Rotate daily
	Daily   bool  `json:"daily"`
	Maxdays int64 `json:"maxdays"`

	Rotate bool `json:"rotate"`

	Level int `json:"level"`
	// contains filtered or unexported fields
}

FileLogWriter implements LoggerInterface. It writes messages by lines limit, file size limit, or time frequency.

func (*FileLogWriter) Destroy

func (w *FileLogWriter) Destroy()

destroy file logger, close file writer.

func (*FileLogWriter) DoRotate

func (w *FileLogWriter) DoRotate() error

DoRotate means it need to write file in new file. new file name like xx.log.2013-01-01.2

func (*FileLogWriter) Flush

func (w *FileLogWriter) Flush()

flush file logger. there are no buffering messages in file logger in memory. flush file means sync file from disk.

func (*FileLogWriter) Init

func (w *FileLogWriter) Init(jsonconfig string) error

Init file logger with json config. jsonconfig like:

{
"filename":"logs/beego.log",
"maxlines":10000,
"maxsize":1<<30,
"daily":true,
"maxdays":15,
"rotate":true
}

func (*FileLogWriter) WriteMsg

func (w *FileLogWriter) WriteMsg(msg string, level int) error

write logger message into file.

type LoggerInterface

type LoggerInterface interface {
	Init(config string) error
	WriteMsg(msg string, level int) error
	Destroy()
	Flush()
}

LoggerInterface defines the behavior of a log provider.

func NewAccessWriter

func NewAccessWriter() LoggerInterface

create a AccessLogWriter returning as LoggerInterface.

func NewConn

func NewConn() LoggerInterface

create new ConnWrite returning as LoggerInterface.

func NewConsole

func NewConsole() LoggerInterface

create ConsoleWriter returning as LoggerInterface.

func NewFileWriter

func NewFileWriter() LoggerInterface

create a FileLogWriter returning as LoggerInterface.

func NewSmtpWriter

func NewSmtpWriter() LoggerInterface

create smtp writer.

type MuxWriter

type MuxWriter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

an *os.File writer with locker.

func (*MuxWriter) SetFd

func (l *MuxWriter) SetFd(fd *os.File)

set os.File in writer.

func (*MuxWriter) Write

func (l *MuxWriter) Write(b []byte) (int, error)

write to os.File.

type OLogger

type OLogger struct {
	// contains filtered or unexported fields
}

func NewLogger

func NewLogger(channellen int64) *OLogger

NewLogger returns a new OLogger. channellen means the number of messages in chan. if the buffering chan is full, logger adapters write to file or other way.

func (*OLogger) Access

func (ol *OLogger) Access(msg string)

log access message.

func (*OLogger) Close

func (bl *OLogger) Close()

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

func (*OLogger) Critical

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

log critical level message.

func (*OLogger) Debug

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

log debug level message.

func (*OLogger) DelLogger

func (bl *OLogger) DelLogger(adaptername string) error

remove a logger adapter in OLogger.

func (*OLogger) EnableFuncCallDepth

func (bl *OLogger) EnableFuncCallDepth(b bool)

enable log funcCallDepth

func (*OLogger) Error

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

log error level message.

func (*OLogger) Flush

func (bl *OLogger) Flush()

flush all chan data.

func (*OLogger) Info

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

log info level message.

func (*OLogger) Log

func (ol *OLogger) Log(tag, format string, v ...interface{})

func (*OLogger) Printf

func (bl *OLogger) Printf(format string, v ...interface{})

for gorp

func (*OLogger) SetLevel

func (bl *OLogger) SetLevel(l int)

set log message level. if message level (such as LevelTrace) is less than logger level (such as LevelWarn), ignore message.

func (*OLogger) SetLogFuncCallDepth

func (bl *OLogger) SetLogFuncCallDepth(d int)

set log funcCallDepth

func (*OLogger) SetLogger

func (bl *OLogger) SetLogger(adaptername string, config string) error

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

func (*OLogger) SetPrefix

func (bl *OLogger) SetPrefix(p string)

set log prefix

func (*OLogger) Trace

func (bl *OLogger) Trace(format string, v ...interface{})

log trace level message.

func (*OLogger) Warn

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

log warn level message.

func (*OLogger) WirteRightNow

func (bl *OLogger) WirteRightNow(msg string, level int)

outputs directly.

type SmtpWriter

type SmtpWriter struct {
	Username           string   `json:"Username"`
	Password           string   `json:"password"`
	Host               string   `json:"Host"`
	Subject            string   `json:"subject"`
	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()

implementing method. empty.

func (*SmtpWriter) Flush

func (s *SmtpWriter) Flush()

implementing method. empty.

func (*SmtpWriter) Init

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

init smtp writer with json config. config like:

{
	"Username":"example@gmail.com",
	"password:"password",
	"host":"smtp.gmail.com:465",
	"subject":"email title",
	"sendTos":["email1","email2"],
	"level":LevelError
}

func (*SmtpWriter) WriteMsg

func (s *SmtpWriter) WriteMsg(msg string, level int) error

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

Jump to

Keyboard shortcuts

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