-
Notifications
You must be signed in to change notification settings - Fork 0
Log module
The Log
module is based on log4cxx library (c++ port of the popular log4j Java logging library) and therefore loggers can be configured in the same way and support the same functionality brought by log4cxx/log4j.
Log.init(string configPath)
Initializes the log module using the provided configuration file. This file must be a valid log4cxx\log4j configuration property file.
A default configuration can be found in the conf\
directory of TINN (the default configuration defines a log file under log/tinn.log
).
The OS.init
function must be called one time only in the main thread and then once initialized the library can be used accross all threads (log4cxx is thread safe).
When initialization fails (configuration file not found or wrong syntax) an exception is thrown.
The following code shows how to initialize the log module using the default configuration file:
Log.init("conf/log.properties");
Log.info(string text, [string logger])
Write text
in the logfile with INFO
log level.
If no logger
name is provided then the default logger (root logger) is used and according to the default configuration the text will be logged to log/tinn.log
.
By providing a logger
name it is possible to log to a different log files other than the default one. This can be done by associating a specific appender to the logger in the configuration file.
The script examples/twologfiles.js
that comes with TINN uses the examples/log_twologfiles.properties
configuration file and shows how to define and use two different logfiles, one for the root logger and one for a custom logger.
This is the content of examples/twologfiles.js
:
Log.init("examples/log_twologfiles.properties");
Log.info("this will be logged to mylogger.log", "mylogger");
Log.info("this will be logged to tinn.log");
Log.warn(string text, [string logger])
Same as Log.info
but it writes log entries with the WARN
log level.
Log.debug(string text, [string logger])
Same as Log.info
but it writes log entries with the DEBUG
log level.
Log.error(string text, [string logger])
Same as Log.info
but it writes log entries with the ERROR
log level.
Log.trace(string text, [string logger])
Same as Log.info
but it writes log entries with the TRACE
log level.
Log.fatal(string text, [string logger])
Same as Log.info
but it writes log entries with the FATAL
log level.
Log.setLevel(integer level, [string logger])
Sets the log level to the value provided.
If no logger
is provided then the log level is set on the root logger.
Log levels are ordered integer numbers that define the severity/importance of log entries. Once a specific level is set, then all log calls with a lower level will be ignored and the corresponding log entries will not be logged to the file.
The following constants are available:
- Log.ALL
- Log.TRACE
- Log.DEBUG
- Log.INFO
- Log.WARN
- Log.ERROR
- Log.FATAL
- Log.OFF
By default the log level is set to DEBUG
.
The following code sets the log level to INFO
, this results in all entries with levels TRACE
and DEBUG
to be filtered out:
Log.setLevel(Log.INFO);
Log.isLevel(integer level, [string logger])
Returns true|false
indicating whether or not log calls with the specified level
will be logged to the file.
This function is useful because sometimes while in DEBUG
you may want to log a large object which results in some
relevant CPU work to serialize it. In this case using Log.isLevel
makes it possible to prevent the serialization of the object when the log entry will be filtered out because a lavel higher than DEBUG
is set.
In the example below, largeObj
will be serialized only when log level is set to DEBUG
or lower:
var largeObj = {....};
if (Log.isLevel(Log.DEBUG)) {
Log.debug("this is how largeObj looks like: " + JSON.stringify(largeObj));
}