Functions to write logs in file disk for V.
I created this library to log behavior oriented data when my user browse my web app for debug purposes.
- Can log in a single file, or file per days (daily)
- Prefix logs with the date and time (with microseconds) and the log severity (see Wikipedia Severity levels)
v install khalyomede.filelog
In this example, we will log a debug message.
import khalyomede.filelog { FileLog, LogSaveMode }
fn main() {
log := FileLog{
folder: "logs",
file_name: "app",
save_mode: LogSaveMode.single,
}
log.debug("user accessed product #12")
}
If you open the file "logs/app.log", this is what you will see:
[2021-05-24 22:12:00.635101] [DBUG] user accessed product 12
In this example, we will log in a file that will be "rotate" per day.
import khalyomede.filelog { FileLog, LogSaveMode }
fn main() {
log := FileLog{
folder: "logs",
file_name: "app",
save_mode: LogSaveMode.daily,
}
log.debug("user 12 disconnected")
}
If you open the file logs/app-2021-05-24.log
, this is what you will see:
[2021-05-24 22:12:00.635101] [DBUG] user 12 disconnected
The log will automatically append logs in a file prefixed by the date of the writting.
In this example, we will log a message using a given severity.
import khalyomede.filelog { FileLog, LogSaveMode, LogSeverity }
fn main() {
log := FileLog{
folder: "logs",
file_name: "app",
save_mode: LogSaveMode.single,
}
log.log("session started", LogSeverity.notice)
}
If you open the file logs/app.log
, this is what you will see:
[2021-05-24 22:12:00.635101] [NTCE] session started
Here is all the available severities:
LogSeverity.debug
LogSeverity.info
LogSeverity.notice
LogSeverity.warning
LogSeverity.error
LogSeverity.critical
LogSeverity.alert
LogSeverity.emergency
If you don't want to pass them manually, use the shorthand functions instead:
import khalyomede.filelog { FileLog, LogSaveMode }
fn main() {
log := FileLog{
folder: "logs",
file_name: "app",
save_mode: LogSaveMode.single,
}
log.debug("some message")
log.info("some message")
log.notice("some message")
log.warning("some message")
log.error("some message")
log.critical("some message")
log.alert("some message")
log.emergency("some message")
}
v test .