The perfect log file

The humble and ever so common log file can come in may shapes, types and forms. From .txt to .log, .stderr or .stdout. They are the history and result of happenings on an operating system, server, program or process. Log files play an informative role and one that is often a saviour.

Whether it be an error, a result, a failure, a success or just a typical notice log files still need to contain certain values and be formatted in a precise way.

Confirmation for the reasons and idea included in this post can be seen at: Example log file (ibm.com), Log files Apache (ibm.com), Data logger (wikipedia.org) and Example log file templates (sap.com).

Folder and File name

The consensus for what is getting logged such as errors, information, outputs or results. Using the folder as this name is also often done with the log file itself the date for the first line. This method results in smaller log files but many more files overall.

error_log.log

/logs/chat/04-11-19.log

In most cases you do not want public access to your log files. Keeping them secure, hidden and unreadable is highly important.

Date time

The first value that is logged, the date and time. There are many ways to format or style your date time value from short and basic to longer and readable.

2:41:52 PM Monday 6th May 2019

Monday 6th May 2019 2:41:52 PM

14:41:52 06/05/2019

06/05/2019 14:41:52

06-06-2019 14:41:52 (Europe/Berlin)

The most important thing is that the date time is there and that it gives the whole value including seconds for precision. The time zone may also need to be included if there is a chance of different time zones being logged into the same file.

Reason

What does this line say? Why did this line get written?

Was there an error or maybe just a process that succeeded, the next value alongside your date time needs to be a short notation for what the line entails such as:

INFO:

SUCCESS:

FAIL:

ERROR:

WARNING:

NOTE:

Clear and to the point that when skimming the log you can pick this up easily and decide if you need to read across.

Description

Short and to the point for the reason this is being logged.

function_name() Compilation failed: invalid range in character class at offset 4

settcpimage: Get TCP images rc - EDC8112I Operation not supported on socket

Run in 1.285 seconds

It may seem that some logs are pointless and that they never get read which could be true… it still needs to be logged. What if something goes wrong or something changes. You can look back and see that a process used to take 1 second now it takes 4 and optimization needs to be done.

Logs can be seen like code comments sometimes, occasionally pointless BUT they are useful to read and see the process and result.

Location/file/process/user

This is the file or user that caused the log maybe even a location or process. This goes last on the line because you know the when (time) the what (log reason) and now the where.

in C:\localhost\app\src\verify.php on line 138

at /export/home/live/ap/htdocs/test 

More specifically including the page number to pinpoint the source. This can also be optional if the log file is only dedicated to the one or only process/program such as a MySQL error log file vs a PHP error log file that covers many files giving logs.

End result

06/05/2019 14:41:52 INFO: update_users() took 5.72 seconds user_functions.php

06/05/2019 14:41:52 WARNING: update_users() took 21.8 seconds user_functions.php
03/22 08:51:06 WARNING:.....mailslot_create: setsockopt(MCAST_ADD) failed - EDC8116I Address not available.