Logging in JSON format
PingFederate can write logs in JSON format using the jog4j2 logging library. In addition to being easily human-readable, JSON is a common logging format for security information and event management (SEIM) security tracking systems.
About this task
You can find JSON log templates in the <pf-install>/server/default/conf/log4j/json-templates directory.
PingFederate includes JSON templates for the following log files:
-
admin-api.log -
admin-audit.log -
admin-event-detail.log -
console.log -
provisioner-audit.log -
provisioner-channel-summary.log -
provisioner.log -
runtime-api.log -
server.log -
thread-pool-exhaustion-dump.log -
transaction.log
The following are not log4j2-enabled, so no JSON log templates are provided:
-
init.log -
jvm-garbage-collection.log -
request.log
|
The If you want only one format, commment out the appender for the other format. If you want both standard and JSON formatted logs, you should use different filenames for each format. Otherwise, both formats will be interwoven in the same file. |
Steps
-
Open the
<pf-install>/pingfederate/server//default/conf/log4j2.xmlfile in a text editor. -
(Optional) For each
JsonTemplateLayoutvalue, designate the URI location of the desired JSON templates.The
${sys:pf.log4j.json.templates.uri}URI designates the default location where the JSON log file templates are stored.You can replace this with a custom URI filepath.Otherwise, log files are stored in their default location of<pf-install>/server/default/conf/log4j/json-templates. -
For each log appender, uncomment the
appender-reffor the JSON format output. -
For each log appender, comment out the
appender-reffor the non-JSON format output.Doing this will avoid PingFederate writing both JSON and rolling file formats to the same log file. -
Save and close the
log4j2.xmlfile.
Custom log patterns
To support custom log patterns in log4j2 logs using JSON output format, you must use special syntax.
For example, if a log file appender references the custom HTTP header using %header to log Content-Type:
<RollingFile ... >
<PatternLayout>
<pattern>%d | %header{Content-Type} | %m%n</pattern>
</PatternLayout>
...
</RollingFile>
In the corresponding JSON template, you must refer to the %header{Content-Type} using the following JSON object:
{
"instant": {
"$resolver": "timestamp",
"pattern": {
"format": "yyyy-MM-dd'T'HH:mm:ss.SSSXX"
}
},
"headerContentType": {
"$resolver": "pattern",
"pattern": "%header{Content-Type}"
}
}
You can find the reference to the relevant JSON template in the log4j2.xml file. The JSON file appender names typically include a -JSON suffix. The associated eventTemplateUri value indicates the relevant JSON template name.
<RollingFile name="RuntimeApiAudit-JSON" ...>
<JsonTemplateLayout eventTemplateUri="file://${sys:pf.conf.dir}/log4j/json-templates/runtime-api-log.json"/>
...
</RollingFile>
Creating custom JSON templates
You can customize JSON log outputs in two ways:
-
Change existing log templates to include or exclude particular event fields.
-
Create new log templates to include the event fields that you want to log.
You can include any JSON event field, as long as it is formatted in the Log4j template syntax.
Learn more about Log4j template syntax in the Log4j documentation.
You can include PingFederate-specific event fields by using the syntax in the Custom log patterns section. You can find PingFederate-specific fields in the log4j2.xml file in PatternLayout containers.
Steps
-
In the
<pf-install>/server/default/conf/log4j/json-templatesdirectory, create a copy of the desired JSON log template file, and give the new file a relevant name. -
Modify the new template file with the JSON fields and formats that you want to log.
-
Modify the
log4j2.xmlfile to reference the new template.For example, if you’re modifying the JSON server log to reference a new template named
server-log-custom.json, add the following to thelog4j2.xmlfile:Example:
<RollingFile name="FILE-JSON" fileName="${sys:pf.log.dir}/server.log" filePattern="${sys:pf.log.dir}/server.log.%i" ignoreExceptions="false"> <JsonTemplateLayout eventTemplateUri="${sys:pf.log4j.json.templates.uri}/server-log-custom.json"/> ... </RollingFile> -
Ensure that the appender is referenced for use by a logger. For this example there should be am uncommented
appender-refthat refers to theFILE-JSONrolling file appender where the custom JSON template is located.