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 ¶
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) GetTimestampForLog ¶
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.