You can use the APIs described below to log information in components or services written using Node.js. Use nodejs-module-webos-pmlog v3.0.1-1 or higher.
APIs for Components and Services
The following two APIs are provided for logging from a Node.js component.
PmLogLib API
This API supports the entire PmLogLib capability, for code written specifically for webOS OSE.
API
Description and Syntax
context.log
Provides the logging mechanism for various levels.
Syntax:
context.log(level, msgid, kv_pairs, msg);
Compatibility API
This API provides a bridge between the JavaScript-standard “console” object, and PmLogLib. This cross-platform libraries will have at least minimal compatibility with the logging system, so we do not lose important log messages.
API
Description and Syntax
console.error
Logs an error message.
Syntax:
console.error();
console.warn
Logs a warning message, but also displays a yellow warning icon next to the logged message.
Syntax:
console.warn();
console.log
Logs a message in the console.
Syntax:
console.log();
console.info
Logs a usage message, but also displays a blue icon next to the logged message.
Syntax:
console.info();
Parameters
The table below provides the description of the message elements. The logging levels will be provided as constants on the pmloglib object.
Parameter
Type
Description
level
Number
The level should be one of the following values:
pmloglib.LOG_CRITICAL: Use this when you need to log critical errors.
pmloglib.LOG_ERR: Use this when you need to log errors.
pmloglib.LOG_WARNING: Use this when you need to log warnings.
pmloglib.LOG_INFO: Use this when you need to log usage metrics.
pmloglib.LOG_DEBUG: Use this when you need to log debug messages. These messages will not get into system log by default. Developers must enable them.
Note This parameter is only required if you are using the generic function of the logging API which does not already specify the level.
msgid
String
The msgid is an arbitrary short string (in most cases between 5 and 16 characters long) that uniquely identifies a log message within a component. The msgid cannot be a NULL or an empty string. It cannot contain a blank space (" ") or curly brackets ("{}") in it.
Every log statement, except for the debug and trace log statement, is expected to have a unique msgid to clearly differentiate the messages by function and use them for metrics analysis. For example, the number of JS service activations per session, can be determined by counting the messages with msgid. The selection of the message IDs is left up to the developer. Typically it would be a short string in all capitals, for example, APPSTRT. It is not necessary that the meaning of the message is apparent from the msgid alone, but it is best to standardize on the names.
It may be an additional burden on the developers to add message IDs to log statements. However, considering that our logs at or above INFO should only include significant information, we expect that programs will not need a large number of message IDs and the effort required will be kept minimal.
Note The msgid is not required if the level parameter is set to debug. Since debug level logs are used by developers for code debugging and not used for metrics analysis, it is kept simpler for developers to log as a free-text at the debug level.
kv_pairs
Object
This is an optional parameter which consists of a set of key-value pairs followed by a free text to provide additional information in the logs which can be used for analytics purpose.
Note This parameter is optional, as long as the message ID itself is descriptive. For example, a message with an ID of NETWORK_DOWN or READ_CONF_FAIL is self-descriptive and does not need additional information passed as a parameter.
Info and the higher level of messages should use key-value pairs to provide useful information about the log.
Keys should NOT contain a colon (":") in it.
msg
String
The text to be logged.
Example
The following is the sample code for how services written in Node.js can log information.
For PmLogLib
var pmloglib = require('pmloglib');
var context =new pmloglib.Context("com.webos.mycomponent");
context.log(pmloglib.LOG_ERR, "APPCRASH", {"APP_NAME":"Facebook", "APP_ID":12}, "Facebook app crashed, restart application");
For Compatibility
var pmloglib = require('pmloglib');
var console =new pmloglib.Console("com.webos.myComponent");
console.log("log message");
console.error("error message");