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('Message')
p6.log.trace(['content':'Message'])
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 {
} catch(Exception ex) {
p6.log.trace('Message', ex)
}
debug¶
Generate a log message with the DEBUG level.
Syntax
void p6.log.debug(Object message)
Example
p6.log.debug('Message')
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 {
} catch(Exception ex) {
p6.log.debug('Message', ex)
}
info¶
Generate a log message with the INFO level.
Syntax
void p6.log.info(Object message)
Example
p6.log.info('Message')
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 {
} catch(Exception ex) {
p6.log.info('Message', ex)
}
warn¶
Generate a log message with the WARN level.
Syntax
void p6.log.warn(Object message)
Example
p6.log.warn('Message')
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 {
} catch(Exception ex) {
p6.log.warn('Message', ex)
}
error¶
Generate a log message with the ERROR level.
Syntax
void p6.log.error(Object message)
Example
p6.log.error('Message')
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 {
} catch(Exception ex) {
p6.log.error('Message', 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 {
} catch(Exception ex) {
final optionsMap = [ userId: 'john.doe', action: 'delete' ]
p6.log.create 'Message1' error( ex )
p6.log.create 'Message2' 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
void p6.log.with(String key, Object value)
Example
p6.log.with 'fee', 'foo'
p6.log.debug 'Debug message'
# Output: DEBUG fee="foo" message="Debug message"
final optionsMap = [ tenantId: '302400' ]
p6.log.with 'fee', 'foo' with 'bar', 'bee'
p6.log.create 'Warning!' warn {
options optionsMap
}
p6.log.debug 'Debug'
# Output: WARN bar="bee" fee="foo" tenantId="302400" message="Warning!"
# Output: DEBUG bar="bee" fee="foo" message="Debug"
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
void p6.log.without(String key)
Example
p6.log.with 'fee', 'foo'
p6.log.debug 'Message 1'
p6.log.without 'fee'
p6.log.debug 'Message 2'
# Output: DEBUG fee="foo" message="Message 1"
# Output: DEBUG message="Message 2"