dailylogger

package module
v0.0.0-...-11a15c6 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2020 License: MIT Imports: 8 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

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

Writer satisfies the io.Writer interface and writes data to a log file. The name of the logfile contains a datestamp in yyyy-mm-dd format. When the logger is created the caller can specify leading and trailing text. For example the name for a logfile created on the 5th October 2020 with leader "data" and trailer "log" would be "data.20201005.log".

The Writer rolls the log over at midnight at the start of each day - it closes yesterday's log and creates today's.

On start up, the first call of New creates today's log file if it doesn't already exist. If the file has already been created, the Writer appends to the existing contents.

The Writer contains a mutex. It's dangerous to copy an object that contain a mutex, so you should always call its methods via a pointer. The New function returns a pointer, so that's a good way to create a DailyLogger.

To allow for good unit testing the Writer's notion of the time of day is controlled by a Clock object. The New factory function creates a Clock which is a thin wrapper around the standard time service. That should be used by production code. Tests can use a clock object that supplies predefined values.

func New

func New(logDir, leader, trailer string) *Writer

New creates a Writer, starts the log rotator and returns the writer. Production code should call this to get a Writer.

func (*Writer) DisableLogging

func (dw *Writer) DisableLogging()

DisableLogging switches logging off - Write calls do nothing.

func (*Writer) EnableLogging

func (dw *Writer) EnableLogging()

EnableLogging switches logging on.

func (*Writer) GetTimestampForLog

func (dw *Writer) GetTimestampForLog() string

GetTimestampForLog gets the timestamp for a log entry, for example "2020-02-14 15:42:11.789 UTC". This uses the real time, not the supplied clock.

func (*Writer) Write

func (dw *Writer) Write(buffer []byte) (int, error)

Write writes the buffer to the daily log file, creating the file at the start of each day.

Jump to

Keyboard shortcuts

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