Logging
Helper Class for Logging
Examples
Simple example with using a DocFile
context.enableModules();
const docFile = context.getFileById("fileId-12345");
const { Logging } = require("ou.sp.Logging");
const logger = new Logging("someCategory");
logger.info("Processing now file", docFile);
Will produce following log
→ Client 940: 102008: [T940] DEBUG someCategory fileId-12345 Some debug message.
You don't need to provide a file-Id. The script will automatically lookup for context.file.
// context.file with fileId "fileId-12345"
context.enableModules();
const { Logging } = require("ou.sp.Logging");
const logger = new Logging("someCategory");
logger.debug("Some debug message");
Will produce following log
→ Client 940: 102008: [T940] DEBUG someCategory fileId-12345 Some debug message.
Constructors
Constructor
new Logging(
category):Logging
Creates a new instance
Parameters
category
string
e.g. script and function name
Returns
Logging
Properties
logs
readonlylogs:object[]
Array which holding the logs of current instance.
message
message:
string
level
level:
LogLevels
Methods
debug()
debug(
message,fileOrFileId?):void
Log a message with DEBUG Level.
Parameters
message
string
Custom message
fileOrFileId?
any
Optional File or File-Id
Returns
void
Example
Example
context.enableModules();
const { Logging } = require("ou.sp.Logging");
const logger = new Logging("someCategory");
logger.debug("Some debug message", "fileId-12345");
Will produce following log
→ Client 940: 102008: [T940] DEBUG someCategory fileId-12345 Some debug message.
info()
info(
message,fileOrFileId?):void
Log a message with INFO Level.
Parameters
message
string
Custom message
fileOrFileId?
any
Optional File or File-Id
Returns
void
Example
Example
context.enableModules();
const { Logging } = require("ou.sp.Logging");
const logger = new Logging("someCategory");
logger.info("Some info message", "fileId-12345");
Will produce following log
→ Client 940: 102008: [T940] INFO someCategory fileId-12345 Some info message.
warn()
warn(
message,fileOrFileId?):void
Log a message with WARN Level.
Parameters
message
string
Custom message
fileOrFileId?
any
Optional File or File-Id
Returns
void
Example
Example
context.enableModules();
const { Logging } = require("ou.sp.Logging");
const logger = new Logging("someCategory");
logger.warn("Some warning message", "fileId-12345");
Will produce following log
→ Client 940: 102008: [T940] WARN someCategory fileId-12345 Some warning message.
error()
error(
message,fileOrFileId?):void
Log a message with ERROR Level.
Parameters
message
string
Custom message
fileOrFileId?
any
Optional File or File-Id
Returns
void
Example
Example
context.enableModules();
const { Logging } = require("ou.sp.Logging");
const logger = new Logging("someCategory");
logger.error("Some error message", "fileId-12345");
Will produce following log
→ Client 940: 102008: [T940] ERROR someCategory fileId-12345Some error message
→ Name: Error
→ Script: otrLogger
→ Line: 737
→ Column: 6
→ Stacktrace:
→ logAsException@otrLogger:737
→ @otrLogger:375
→ Logging.prototype.log@ou.sp.Logging:436
→ Logging.prototype.error@ou.sp.Logging:369
→ __Janus_0@ou.sp.example:4
→ .
fatal()
fatal(
message,fileOrFileId?):void
Log a message with FATAL Level.
Parameters
message
string
Custom message
fileOrFileId?
any
Optional File or File-Id
Returns
void
Example
Example
context.enableModules();
const { Logging } = require("ou.sp.Logging");
const logger = new Logging("someCategory");
logger.fatal("Some fatal message", "fileId-12345");
Will produce following log
→ Client 940: 102008: [T940] FATAL someCategory fileId-12345Some fatal message
→ Name: Error
→ Script: otrLogger
→ Line: 737
→ Column: 6
→ Stacktrace:
→ logAsException@otrLogger:737
→ @otrLogger:432
→ Logging.prototype.log@ou.sp.Logging:440
→ Logging.prototype.fatal@ou.sp.Logging:380
→ __Janus_0@ou.sp.example:4
→ .
time()
time(
label):void
Starts a timer provided by a unique identifier
Parameters
label
string
Unique identifier with which the timer can be identified.
Returns
void
Example
Example
context.enableModules();
const { Logging } = require("ou.sp.Logging");
const logger = new Logging("someCategory");
logger.time("timer1");
// ...
logger.timeEnd("timer1");
Will produce following log
→ Client 962: 102008: [T962] INFO Timer The operation "timer1" took 1 ms..
timeEnd()
timeEnd(
label):void
Ends a timer provided by a unique identifier
Parameters
label
string
Unique identifier with which the timer can be identified.
Returns
void
Example
Example
context.enableModules();
const { Logging } = require("ou.sp.Logging");
const logger = new Logging("someCategory");
logger.time("timer1");
// ...
logger.timeEnd("timer1");
Will produce following log
→ Client 962: 102008: [T962] INFO Timer The operation "timer1" took 1 ms..
log()
log(
message,level,fileOrFileId?):void
Logs a message
Parameters
message
string
Custom message
level
LogLevels
Specified log level "DEBUG" | "INFO" | "WARN" | "ERROR" | "FATAL" | "NONE"
fileOrFileId?
any
Optional File or File-Id
Returns
void
toString()
toString():
string
Returns a string with all logs used in this instance.
Returns
string
String with all logs used in this instance.
Example
Example
context.enableModules();
const { Logging } = require("ou.sp.Logging");
const logger = new Logging("someCategory");
logger.debug("Some debug message", "fileId-12345");
logger.info("Some info message", "fileId-12345");
context.returnValue = logger.toString();
Will return following String
DEBUG: (someCategory) Some debug message
INFO: (someCategory) Some info message
toJSON()
toJSON():
string
Returns all logs used in this instance as JSON-string array.
Returns
string
A JSON-string
Example
Example
context.enableModules();
const { Logging } = require("ou.sp.Logging");
const logger = new Logging("someCategory");
logger.debug("Some debug message", "fileId-12345");
context.returnValue = logger.toJSON();
Will return following JSON-String
[
{
"message": "Some debug message",
"level": "DEBUG"
}
]
use()
staticuse(category):Logging
Creates a new Logging class instance
Parameters
category
string
e.g. script and function name
Returns
Logging
A instance of the Logging class
Example
Example
context.enableModules();
const logger = require("ou.sp.Logging").Logging.use("someCategory");
logger.debug("Some debug message", "fileId-12345");
Will produce following log
→ Client 940: 102008: [T940] DEBUG someCategory fileId-12345 Some debug message.