Skip to content

Log module

Francesco Saverio Castellano edited this page Mar 18, 2020 · 25 revisions

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 XML.
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/Log4cxxConfig.xml");

 
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/Log4cxxConfig_two_logfiles.xml 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/Log4cxxConfig_two_logfiles.xml");
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.  
 

Clone this wiki locally