Logging
Programs that run without a person watching need a diary. This lesson writes a logger class with levels, timestamps, a file and rotation.
What you will learn#
- what to log and at which level,
- appending lines to a file,
- the minimum level (development versus production),
- logging an error you caught,
- rotation: a log must not grow forever.
The program#
# logging - levels, timestamps, a file, rotation, and what to write
load "dotnet.ring"
using {
"System"
"System.IO"
}
func main
cFolder = new Path().Combine([new Path().GetTempPath(),"ringdotnet_logging"])
if new Directory().Exists(cFolder)
new Directory().Delete([ :path = cFolder, :recursive = True ])
ok
new Directory().CreateDirectory(cFolder)
cFile = new Path().Combine([cFolder,"app.log"])
# 1. A logger writes lines: time, level, message. The level says how important a line is.
oLog = new Logger(cFile)
oLog.setClock("2024-05-17 08:00:00") # a fixed time: the output of the tutorial is the same every run
oLog.info("the program started")
oLog.debug("a detail: this line is not written (the minimum level is INFO)")
oLog.warn("the disk is 91% full")
oLog.error("could not open the report")
? "1. The file after four calls:"
? " " + substr(new File().ReadAllText(cFile),windowsnl(),nl + " ")
# 2. The minimum level: a program in development wants everything, a program in production only problems
oLog.setLevel("DEBUG")
oLog.debug("now debug lines are written too")
oLog.setLevel("ERROR")
oLog.info("this line is NOT written")
oLog.error("this one is")
? nl + "2. Lines in the file now: " + len(new File().ReadAllLines(cFile).toList())
# 3. Errors are the reason for logs: write the error text of a failure, and keep going
try
new Int32().Parse("abc")
catch
oLog.error("a number could not be read: " + left(cCatchError,60))
done
? nl + "3. The last line: " + new File().ReadAllLines(cFile).toList()[len(new File().ReadAllLines(cFile).toList())]
# 4. Rotation: a log must not grow forever. When the file is too big it is renamed and a new one starts.
oSmall = new Logger(new Path().Combine([cFolder,"small.log"]))
oSmall.setClock("2024-05-17 09:00:00")
oSmall.setMaxSize(300) # bytes: tiny, to see the rotation
for nLine = 1 to 12
oSmall.info("message number " + nLine + " with some more text to fill the file")
next
? nl + "4. Files in the folder after 12 messages with a limit of 300 bytes:"
aNames = sort(new Directory().GetFiles([ :path = cFolder, :searchPattern = "small*" ]).toList())
for cName in aNames
? " " + new Path().GetFileName(cName)
next
new Directory().Delete([ :path = cFolder, :recursive = True ])
# A logger as a Ring class: the settings are attributes, the behavior is in the methods
class Logger
cFile = ""
cClock = "" # when set, this text is the time of every line (for tests); empty: the real time
nLevel = 2 # DEBUG = 1, INFO = 2, WARN = 3, ERROR = 4
nMaxSize = 0 # 0: no limit
func init cPath
cFile = cPath
func setClock cText
cClock = cText
func setLevel cLevel
nLevel = levelNumber(cLevel)
func setMaxSize nBytes
nMaxSize = nBytes
func debug cMessage
write("DEBUG",cMessage)
func info cMessage
write("INFO ",cMessage)
func warn cMessage
write("WARN ",cMessage)
func error cMessage
write("ERROR",cMessage)
func levelNumber cLevel
cLevel = upper(trim(cLevel))
if cLevel = "DEBUG"
return 1
but cLevel = "INFO"
return 2
but cLevel = "WARN"
return 3
ok
return 4
# one line: "2024-05-17 08:00:00 INFO the program started"
func write cLevel, cMessage
if levelNumber(cLevel) < nLevel
return
ok
rotateIfNeeded()
cTime = cClock
if cTime = ""
cTime = new DateTime(2000,1,1).Now.Format("yyyy-MM-dd HH:mm:ss")
ok
new File().AppendAllText([ :path = cFile, :contents = cTime + " " + cLevel + " " + cMessage + windowsnl() ])
# when the file is bigger than the limit: app.log becomes app.log.1 (an older .1 is replaced), and a new file starts
func rotateIfNeeded
if nMaxSize = 0 or not new File().Exists(cFile)
return
but new FileInfo(cFile).Length > nMaxSize
cOld = cFile + ".1"
if new File().Exists(cOld)
new File().Delete(cOld)
ok
new File().Move([ :sourceFileName = cFile, :destFileName = cOld ])
ok1. The file after four calls:
2024-05-17 08:00:00 INFO the program started
2024-05-17 08:00:00 WARN the disk is 91% full
2024-05-17 08:00:00 ERROR could not open the report
2. Lines in the file now: 5
3. The last line: 2024-05-17 08:00:00 ERROR a number could not be read: DotNet Error: System.FormatException: The input string 'abc'
4. Files in the folder after 12 messages with a limit of 300 bytes:
small.log
small.log.1How it works#
A line has a time, a level and a message. The level says how important it is: DEBUG (details for the developer), INFO (normal events), WARN (something is wrong but the program goes on), ERROR (an operation failed). The logger has a minimum level: lines below it are not written, so step 1 skips the debug line, and step 2 shows everything and then only errors.
File.AppendAllText adds a line at the end of the file. The clock can be fixed with setClock so that the output of the tutorial is the same every run; a real logger leaves it empty and uses the real time.
Step 3 shows the main reason for logs: try / catch writes the error text and the program continues.
Rotation (step 4): before each line the logger checks the size (FileInfo.Length). When it is over the limit, small.log is renamed to small.log.1 (an older .1 is deleted first) and a new file starts. The disk never fills with one endless file.
Never write passwords, tokens or personal data in a log. Logs are copied around, and nobody protects them like a database.
What you learned#
- levels and a minimum level,
AppendAllText,- logging caught errors,
- rotation with
FileInfoandFile.Move.
Exercises#
- Keep three old files (
.1,.2,.3). - Add a
fatallevel and a--verboseoption like in the tutorial A command-line tool. - Write a log line for each request of the web server in the tutorial A small web site.