log

package module
v0.0.0-...-455f1a7 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2017 License: MIT Imports: 4 Imported by: 0

README

Log

GoDoc Build Status Coverage Status

Log package

This package is a think wrapper around the standard library package syslog. Since logging is a blocking operation can become quite expensive, specially in log heavy applications. This package is born in order to reduce to minum or totally avoid, blocking in fact offers two main functions Log and SyncLog. The first of the two offers no blocking but does not grant that the message will be logged, SyncLog should instead assure the message will be logged, if case or a local logger ( that is a file logger ) is the syslog case can't be forced to sync. It provides the following:

-New(prefix,logfilename) func that given a prefix string, and file name (optional) construct a Logger, it can fails if for any reason the file can't be opened or created or there is a failure during the syslog instantiation.

-The other provided functions are Log() and SyncLog() which sends the message, the first one send the message in a non blocking manner and the second one as well, though the second one syncs to disk in case of file logger assuring the log is sent.

-Close() closes the logger, it's important to do so because otherwise we would leak the goroutine who does the job in the background.

How to use it

It's pretty simple

  l := log.New("[MYAPPNAME]","") // syslog
  l := log.New("[MYAPPNAME]","local.log") // local file log
  // then in order to log
  l.Log("My message") // logs optmistically
  l.SyncLog("My message") tries to sync to disk in case it's logger with local file
  // very important don't forget to close it once done
  l.Close() 
Important

Due to a design decision this logger is an optimistic logger, that means you can afford to lose log messages, however to minimize the risk when you close the logger in the edge case that your program immediately terminates, please introduce a artificial sleep of at least 1 second.

Philosophy

This software is developed following the "mantra" keep it simple, stupid or better known as KISS. Something so simple like a cache with auto eviction should not required over engineered solutions. Though it provides most of the functionality needed by generic configuration files, and most important of all meaning full error messages.

Disclaimer

This software in alpha quality, don't use it in a production environment, it's not even completed.

Thank You Notes

None.

Documentation

Index

Constants

View Source
const (
	MsgMaxLen = 79
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger struct {
	Messages chan string
	Done     chan bool
	Sync     chan bool
	// contains filtered or unexported fields
}

Logger struct

func New

func New(prefix string, filename string) *Logger

New creates a new logger takes a prefix with is going to be added to every line logged and a filename, if the file is omited and is given an empty string like so "" the local syslog will be used, if everything fails a nil logger will be returned else a valid logger instance is returned.

func (*Logger) Close

func (s *Logger) Close()

Typically called in defer log.Close() fashion for short lived objects otherwise must be called when the logger isn't needed any longer, once called if any messages are still left on the Messages channel, they will be sent to syslog before actually exting, any message sent after the close method is called will a result in a sent operation to a close channel.

func (*Logger) Log

func (s *Logger) Log(msg string)

Log logs the message but doesn't assure that the message it being sent

func (*Logger) SyncLog

func (s *Logger) SyncLog(msg string)

SyncLog logs the message and in case of a file logger syncs it to disk immidiately assuring logs are always written to disk.

Jump to

Keyboard shortcuts

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