Log
Purpose¶
Provides log methods.
Methods¶
Binding name:
p6.log
Warning
Since 6.1.1 - log binding is deprecated
New Feature
Since 6.10.13 - p6.log supports objects as first parameter, which will be converted to a string using toString() method.
Output¶
Every log call will generate:
[5-12-2020 16:40] <LEVEL> [GROOVYSCRIPT: <Logger name>] <Message>
The Logger name is equal to:
- for script:
io.platform6.app.<App Key | core>.scripts.<Script name>.Main - for route:
io.platform6.app.<App Key | core>.routes.<Route name>.Main
Provided exceptions will only be visible inside the application log file and not inside the script output log.
trace¶
Generate a log message with the TRACE level.
Syntax
void p6.log.trace(Object message)
Example
p6.log.trace('Entering invoice validation for INV-2024-0042')
p6.log.trace(['invoiceId': 'INV-2024-0042', 'step': 'validation'])
trace (with exception)¶
Generate a log message, and an exception with the TRACE level.
Syntax
void p6.log.trace(Object message, Throwable exception)
Example
try {
p6.transaction.get(pk)
} catch(Exception ex) {
p6.log.trace('Failed to retrieve transaction', ex)
}
debug¶
Generate a log message with the DEBUG level.
Syntax
void p6.log.debug(Object message)
Example
p6.log.debug('Processing invoice INV-2024-0042 for supplier ACME-CORP')
debug (with exception)¶
Generate a log message, and an exception with the DEBUG level.
Syntax
void p6.log.debug(Object message, Throwable exception)
Example
try {
def xml = p6.pipeline.getXml('invoiceXml')
} catch(Exception ex) {
p6.log.debug('Failed to parse invoice XML from pipeline', ex)
}
info¶
Generate a log message with the INFO level.
Syntax
void p6.log.info(Object message)
Example
p6.log.info('Invoice INV-2024-0042 validated and routed to approval workflow')
info (with exception)¶
Generate a log message, and an exception with the INFO level.
Syntax
void p6.log.info(Object message, Throwable exception)
Example
try {
p6.script.call('O2C.ProcessInvoice')
} catch(Exception ex) {
p6.log.info('Invoice processing script completed with warnings', ex)
}
warn¶
Generate a log message with the WARN level.
Syntax
void p6.log.warn(Object message)
Example
p6.log.warn('Invoice amount exceeds approval threshold: 50000.00 EUR')
warn (with exception)¶
Generate a log message, and an exception with the WARN level.
Syntax
void p6.log.warn(Object message, Throwable exception)
Example
try {
p6.transaction.save(invoiceXml, 'TransactionInfo', pk)
} catch(Exception ex) {
p6.log.warn('Transaction save failed, retrying...', ex)
}
error¶
Generate a log message with the ERROR level.
Syntax
void p6.log.error(Object message)
Example
p6.log.error('Failed to connect to AS2 gateway for partner ACME-CORP')
error (with exception)¶
Generate a log message, and an exception with the ERROR level.
Syntax
void p6.log.error(Object message, Throwable exception)
Example
try {
p6.transaction.saveAndRouteTI(invoiceXml, 'direct:p6router.1', 'INV-2024-0042')
} catch(Exception ex) {
p6.log.error('Critical: invoice routing failed for INV-2024-0042', ex)
}
create¶
New Feature
Since 6.10.17
Creates a logger with a specific message for a given log level. Optionally, an exception and options can be provided.
Syntax
void p6.log.create(Object message) debug { options Map }
void p6.log.create(Object message) info()
void p6.log.create(Object message) error()
void p6.log.create(Object message) error { options Map }
void p6.log.create(Object message) error(Throwable ex), { options Map }
Example
p6.log.create 'Trace message' trace()
p6.log.create 'Debug' debug()
p6.log.create 'Information message' info()
p6.log.create 'Warning!' warn()
p6.log.create 'An error occured' error()
p6.log.create 'An error occured' error(new P6Exception('Custom exception'))
final optionsMap = [ tenantId: '302400', action: 'update' ]
p6.log.create 'Warning!' warn {
options optionsMap
}
p6.log.create 'Conversion error' error(new P6Exception('Custom exception')), {
options optionsMap
}
try {
p6.transaction.remove(pk)
} catch(Exception ex) {
final optionsMap = [ userId: 'john.doe', action: 'delete' ]
p6.log.create 'Transaction removal failed' error( ex )
p6.log.create 'Transaction removal failed with context' error( ex ), {
options optionsMap
}
}
with¶
New Feature
Since 6.10.17
Allows the user to provide a context to the logger to all the next log messages
Syntax
Log p6.log.with(String key, Object value)
Example
p6.log.with 'invoiceId', 'INV-2024-0042'
p6.log.debug 'Starting validation'
# Output: DEBUG invoiceId="INV-2024-0042" message="Starting validation"
final optionsMap = [ tenantId: '302400' ]
p6.log.with 'invoiceId', 'INV-2024-0042' with 'supplier', 'ACME-CORP'
p6.log.create 'Amount exceeds threshold' warn {
options optionsMap
}
p6.log.debug 'Routing to manual approval'
# Output: WARN invoiceId="INV-2024-0042" supplier="ACME-CORP" tenantId="302400" message="Amount exceeds threshold"
# Output: DEBUG invoiceId="INV-2024-0042" supplier="ACME-CORP" message="Routing to manual approval"
without¶
New Feature
Since 6.10.17
Allows the user to remove an element from the logger context to all the next log messages
Syntax
Log p6.log.without(String key)
Example
p6.log.with 'invoiceId', 'INV-2024-0042'
p6.log.debug 'Processing invoice'
p6.log.without 'invoiceId'
p6.log.debug 'Invoice context cleared'
# Output: DEBUG invoiceId="INV-2024-0042" message="Processing invoice"
# Output: DEBUG message="Invoice context cleared"