Commit 2439132f authored by Eirik Alvær's avatar Eirik Alvær

Add support for setting all properties defined in the log schema as input to...

Add support for setting all properties defined in the log schema as input to ExpressLogger log functions.
parent 3c598f99
Pipeline #23741 failed with stage
in 16 seconds
......@@ -4,38 +4,23 @@ import Http = require("http");
import JsonSchema = require("jsonschema");
import _ = require("lodash");
const serviceName = process.env.npm_package_name;
const serviceVersion = process.env.npm_package_version;
const maxStringSize = 1000;
export interface LogInput {
type PartialLogSchema = Partial<LogSchema>
const defaultValues: PartialLogSchema = {
serviceName: process.env.npm_package_name || "unknown",
serviceVersion: process.env.npm_package_version || "unknown"
};
export interface LogInput extends PartialLogSchema {
// Modified props
message: string;
data?: any;
error?: {
type: string;
code: number;
serviceName?: string;
stack?: string;
data?: any;
originalErrorObject?: any;
};
file?: string;
logRequestPayload?: boolean;
responseTime?: number;
response?: {
body?: string;
headers?: Http.OutgoingHttpHeaders
};
// New props
req?: Express.Request;
tags?: string[];
statusCode?: number;
serviceName?: string; // Defaults to name from package.json file when program is started using npm
serviceVersion?: string; // Defaults to version from package.json file when program is started using npm
maxStringSize?: number; // Defaults to 1000
}
export interface FullLogInput extends LogInput {
levelName: LogSchema["levelName"];
maxStringSize?: number;
logRequestPayload?: boolean;
}
function limitStringSize(str: string, limit?: number) {
......@@ -81,20 +66,19 @@ function extractHeaders(headers: Http.IncomingHttpHeaders | Http.OutgoingHttpHea
return res;
}
function getLogData(input: FullLogInput): LogSchema {
const res: LogSchema = {
"@timestamp": new Date().toISOString(),
file: input.file,
levelName: input.levelName,
loggerName: "ExpressLogger",
message: input.message,
responseTime: input.responseTime,
schemaVersion: "v3",
serviceName: input.serviceName || serviceName || "unknown",
serviceVersion: input.serviceVersion || serviceVersion || "unknown",
tags: input.tags,
statusCode: input.statusCode
};
function getLogData(input: LogInput): LogSchema {
let res = _.cloneDeep(defaultValues) as LogInput;
_.merge(res, input);
delete res.req;
delete res.logRequestPayload;
delete res.maxStringSize;
res["@timestamp"] = new Date().toISOString();
res.loggerName = "ExpressLogger";
res.schemaVersion = "v3";
const req = input.req;
if (req) {
res.host = req.hostname;
......@@ -132,7 +116,7 @@ function getLogData(input: FullLogInput): LogSchema {
originalErrorObject: stringify(error.originalErrorObject, input.maxStringSize)
}
}
return res;
return res as LogSchema;
}
function log(levelName: LogSchema["levelName"], input: LogInput) {
......@@ -140,7 +124,7 @@ function log(levelName: LogSchema["levelName"], input: LogInput) {
console.log("----------------------------------------------------------------------------------");
}
try {
const fullLogInput = _.merge({ levelName}, input);
const fullLogInput = _.merge({ levelName }, input);
const data = getLogData(fullLogInput);
const str = stringify(data, Number.MAX_SAFE_INTEGER);
console.log(str);
......@@ -162,19 +146,19 @@ function log(levelName: LogSchema["levelName"], input: LogInput) {
}
export function debug(input: LogInput) {
log ("DEBUG", input);
log("DEBUG", input);
}
export function info(input: LogInput) {
log ("INFO", input);
log("INFO", input);
}
export function warn(input: LogInput) {
log ("WARN", input);
log("WARN", input);
}
export function error(input: LogInput) {
log ("ERROR", input);
log("ERROR", input);
}
export function middleware(req: Express.Request, res: Express.Response, next: Express.NextFunction) {
......@@ -192,9 +176,9 @@ export function middleware(req: Express.Request, res: Express.Response, next: Ex
responseLogged = true;
const responseTime = Date.now() - startTime;
const message = reqInfoString(req) + " " + res.statusCode + " took " + responseTime + " ms.";
const body = _.isString(data)? data : JSON.stringify(data);
const body = _.isString(data) ? data : JSON.stringify(data);
const response = {
headers: res.getHeaders(),
headers: res.getHeaders() as { [k: string]: string },
body
}
info({ responseTime, message, file, req, response });
......
......@@ -79,4 +79,8 @@ Example using the ExpressLogger:
});
```
By using the ExpressLogger middleware basic info about each request and response will be logged.
\ No newline at end of file
By using the ExpressLogger middleware basic info about each request and response will be logged.
## Deploy
When comitting a tag to gitlab the npm module will be deployed as "@nsd/log-schema-node".
\ No newline at end of file
{
"name": "@nsd/log-schema-node",
"version": "3.1.0",
"version": "3.2.0",
"main": "lib/index.js",
"types": "lib/index",
"scripts": {
"compile": "tsc"
"build": "tsc"
},
"repository": "git@gitlab.nsd.uib.no:nsd-commons/log-schema-node.git",
"author": "Snorre Magnus Davøen <snorre.davoen@nsd.uib.no>",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment