1
0
mirror of https://github.com/CFC-Servers/gm_logger.git synced 2025-03-04 03:03:01 -05:00
A feature-rich and delightfully simple logging library for Garry's Mod
Go to file
2019-11-14 16:10:24 -05:00
lua/autorun/server Remove moonscript require 2019-11-14 16:10:24 -05:00
.gitignore Initial commit 2019-09-27 11:32:18 -07:00
LICENSE Initial commit 2019-09-27 11:32:18 -07:00
README.md Update README.md 2019-10-01 17:19:14 -07:00

CFC's Logging Library (BETA)

Installation

Simply download a copy of the zip, or clone the repository straight into your addons folder! (When we finalize the API, we won't release breaking changes to master)

Usage

CFCLogger lets you create and configure your own logging object to be used in your project.

Barebones usage example:

local logger = CFCLogger( "MyProjectName" )

logger:trace( "This is an trace message!" )
logger:debug( "This is an debug message!" )
logger:info( "This is an info message!" )
logger:warn( "This is an warn message!" )
logger:error( "This is an error message!" )
logger:fatal( "This is an fatal message!" )

Which produces this output:

[MyProjectName] [info] This is an info message!
[MyProjectName] [warn] This is an warn message!
[MyProjectName] [error] This is an error message!
[MyProjectName] [fatal] This is an fatal message!

Logging Levels

When a logger is created, one may pass an optional second parameter which defines the current log level of the app. By default it's set to "info" which means everything but debug and trace messages are printed. (trace < debug < info < warn < error < fatal)

Any message sent with a lower log-level than the one defined in the logger object will not be printed.

As an example, if you set the default log level to "error", then only "error" and "fatal" messages are printed.

local logger = CFCLogger( "MyProjectName", "error" )

logger:trace( "This is an trace message!" )
logger:debug( "This is an debug message!" )
logger:info( "This is an info message!" )
logger:warn( "This is an warn message!" )
logger:error( "This is an error message!" )
logger:fatal( "This is an fatal message!" )

Output:

[MyProjectName] [error] This is an error message!
[MyProjectName] [fatal] This is an fatal message!

Callbacks

The logger object allows you to add callbacks to any log-level. If a message comes through with the matching log level, the callback is called.

The callback is provided with one parameter; the contents of the message to be printed (before adding the prefixes like "[MyProject]").

For example, maybe you want to forward all fatal messages to Discord.

The syntax for adding callbacks is as follows:

MyLoggingInstance:on( logLevel ):call( myCallback )

In an example:

local function forwardFatalToDiscord( message )
    -- send the message to discord
    print( "Sending fatal message to discord! ( " .. message .. ")" )
end

local logger = CFCLogger( "MyProjectName" )
logger:on( "fatal" ):call( forwardFatalToDiscord )

logger:fatal( "Major oof!" )

Output:

[MyProjectName] [fatal] Major oof!
Sending fatal message to discord! ( Major oof!)

Please note that the callbacks will run regardless of the default log level setting.

This means that if your log level is set to "error", but you add a callback to "warn", all "warn" messages would not be printed in the console, but the attached callback would fire.

As an example:

local logger = CFCLogger( "MyProject", "fatal" )
logger:on( "warn" ):call(function(message) print("I'm a 'warn' callback!") end)

logger:warn("This is a test!")

Would only output:

I'm a 'warn' callback!