Skip to main content

Logger

The Logger module sets the log level in Moogfarmd, allowing log messages to be written to the common Moogfarmd log file. See Configure Logging for more information on configuring logging.

When you create a Moobot you can use the Logger module for debugging. Writing a log message to a file is an IO operation, and comes with execution cost. When developing a Moobot it can be helpful to include a number of logging statements. Once the Moobot is operational, however, it is best to keep logging to a minimum.

Load the module

The Logger module is available to load into any standard Moobot. To load it, define a new global variable at the top of the Moobot Javascript file. For example:

var logger = MooBot.loadModule('Logger');

Method reference

The Logger module uses the following methods. Note that printf Logger functions have been deprecated in favour of the 'single string argument' version. For more information see the Wikipedia entry for Printf format string.

debug

Sends a debug log message (the lowest severity level). You can use the debug method to log detailed troubleshooting information in non-production environments.

Request arguments

The method takes the following arguments.

Name

Type

Required

Description

logmessage

String

Yes

A log message formed of a single string of JavaScript variables or objects. Use concatenation to form multiple arguments.

Response

None.

Example

Example use of the debug method to log a message:

logger.debug("A debug message");

This example logs the following message:

DEBUG:... ...A debug message

info

Sends an information log message (the intermediate severity level). You can use it to log changing a property, for example.

Request arguments

The method takes the following arguments.

Name

Type

Required

Description

logmessage

String

Yes

A log message formed of a single string of JavaScript variables or objects. Use concatenation to form multiple arguments.

Response

None.

Example

Example use of the info method to log a message:

{
   var dispText= "Reset";
   var dispNum= 2;
   var aReal= 3.141593;

   logger.info("Counter: "+ dispText);
   logger.info("Severity low. Level: "+ dispNum + ". ...Pi = "+ aReal);
}

This example logs the following messages:

INFO :... ...Counter: Reset
INFO :... ...Severity low. Level: 2. ...Pi = 3.141593

warning

Sends a warning message (high severity level). You can use it to log behavior that impacts normal operation of the system.

Request arguments

The method takes the following arguments.

Name

Type

Required

Description

logmessage

String

Yes

A log message formed of a single string of JavaScript variables or objects. Use concatenation to form multiple arguments.

Response

None.

Example

Example use of the warning method to log a message:

{
   var aString= "CPU@ >90%";
   var intHigh= 4;

   logger.warning("Warning: "+ aString);
   logger.warning("Severity high. Level: "+ intHigh);
}

This example logs the following messages:

WARN :... ...Warning: CPU@ >90%
WARN :... ...Severity high. Level: 4

fatal

Sends a fatal log message (the highest severity setting). You can use it to log extreme circumstances, such as an unrecoverable failure that caused Moogfarmd to exit.

Request arguments

The method takes the following arguments.

Name

Type

Required

Description

logmessage

String

Yes

A log message formed of a single string of JavaScript variables or objects. Use concatenation to form multiple arguments.

Response

None.

Example

Example use of the fatal method to log a message:

{
   var intHighest= 5;

   logger.fatal("Severity exceeds "+ intHighest + "! Restart required");
}

This example logs the following message:

FATAL:... ...Severity exceeds 5! Restart required