92 lines
3.2 KiB
JavaScript
92 lines
3.2 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
const UNKNOWN_NUMBER = -1;
|
||
|
const UNKNOWN_STRING = '-';
|
||
|
|
||
|
class CFLog {
|
||
|
constructor(env) {
|
||
|
this._env = env;
|
||
|
}
|
||
|
|
||
|
format(entryCtx) {
|
||
|
const entry = createBaseEntry(entryCtx);
|
||
|
const customFields = entryCtx.customFields;
|
||
|
entry.msg = entryCtx.message;
|
||
|
|
||
|
if (customFields) {
|
||
|
const formattedCustomFields = {
|
||
|
'string': []
|
||
|
};
|
||
|
let index = 0;
|
||
|
|
||
|
for (let key in customFields) {
|
||
|
formattedCustomFields.string.push({
|
||
|
'k': key,
|
||
|
'v': customFields[key],
|
||
|
'i': index++
|
||
|
});
|
||
|
}
|
||
|
|
||
|
entry['#cf'] = formattedCustomFields;
|
||
|
}
|
||
|
return JSON.stringify(entry);
|
||
|
}
|
||
|
|
||
|
formatNetwork(entryCtx) {
|
||
|
let entry = createBaseEntry(entryCtx);
|
||
|
|
||
|
entry['direction'] = entryCtx.reqDirection;
|
||
|
entry['protocol'] = entryCtx.reqProtocol;
|
||
|
entry['method'] = entryCtx.reqMethod;
|
||
|
entry['request'] = entryCtx.reqPath;
|
||
|
entry['request_size_b'] = entryCtx.reqSize || UNKNOWN_NUMBER;
|
||
|
entry['request_received_at'] = entryCtx.reqReceivedAt;
|
||
|
|
||
|
entry['remote_user'] = this._env.logUser ? (entryCtx.user || UNKNOWN_STRING) : 'redacted';
|
||
|
entry['referer'] = this._env.logReferer ? (entryCtx.referer || UNKNOWN_STRING) : 'redacted';
|
||
|
entry['remote_host'] = this._env.logConnectionData ? (entryCtx.remoteHost || UNKNOWN_STRING) : 'redacted';
|
||
|
entry['remote_ip'] = this._env.logConnectionData ? (entryCtx.remoteIp || UNKNOWN_STRING) : 'redacted';
|
||
|
entry['remote_port'] = this._env.logConnectionData ? (entryCtx.remotePort || UNKNOWN_STRING) : 'redacted';
|
||
|
entry['x_forwarded_for'] = this._env.logConnectionData ? (entryCtx.xForwardedFor || UNKNOWN_STRING) : 'redacted';
|
||
|
|
||
|
entry['response_sent_at'] = entryCtx.resSentAt.toDate().toJSON();
|
||
|
entry['response_time_ms'] = entryCtx.resTime;
|
||
|
entry['response_status'] = entryCtx.resStatus || UNKNOWN_NUMBER;
|
||
|
entry['response_size_b'] = entryCtx.resSize || UNKNOWN_NUMBER;
|
||
|
entry['response_content_type'] = entryCtx.resContentType || UNKNOWN_STRING;
|
||
|
|
||
|
return JSON.stringify(entry);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function createBaseEntry(entryCtx) {
|
||
|
let entry = {
|
||
|
'written_at': entryCtx.timeCreated.toDate().toJSON(),
|
||
|
'written_ts': entryCtx.timeCreatedNanos,
|
||
|
'csn_component': entryCtx.csnComponent || UNKNOWN_STRING,
|
||
|
'correlation_id': entryCtx.correlationId || UNKNOWN_STRING,
|
||
|
'type': entryCtx.type,
|
||
|
'logger': entryCtx.loggingToolName,
|
||
|
'layer': entryCtx.component,
|
||
|
'level': entryCtx.level,
|
||
|
|
||
|
'container_id': entryCtx.containerId || UNKNOWN_STRING,
|
||
|
'component_type': entryCtx.componentType,
|
||
|
'component_id': entryCtx.componentId || UNKNOWN_STRING,
|
||
|
'component_name': entryCtx.componentName || UNKNOWN_STRING,
|
||
|
'component_instance': entryCtx.componentInstance || UNKNOWN_NUMBER,
|
||
|
'source_instance': entryCtx.sourceInstance || UNKNOWN_NUMBER,
|
||
|
'organization_id': entryCtx.organizationId || UNKNOWN_STRING,
|
||
|
'organization_name': entryCtx.organizationName || UNKNOWN_STRING,
|
||
|
'space_id': entryCtx.spaceId || UNKNOWN_STRING,
|
||
|
'space_name': entryCtx.spaceName || UNKNOWN_STRING
|
||
|
};
|
||
|
|
||
|
const contextIdProp = entryCtx.hasRequest ? 'request_id' : 'context_id';
|
||
|
entry[contextIdProp] = entryCtx.id;
|
||
|
|
||
|
return entry;
|
||
|
}
|
||
|
|
||
|
module.exports = CFLog;
|