Repository

filelog

Functions to write logs in file disk for V.

Summary

About

I created this library to log behavior oriented data when my user browse my web app for debug purposes.

Features

  • 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 )

Installation

v install khalyomede.filelog

Examples

1. Log in a single file

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

2. Log in a daily file

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.

3. Log using a specific severity

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")
}

Test

v test .

About

0
102
1 year ago

Author

khalyomede