Simple logger for logging applications with the ability to replace sensitive data in logs.
You can install the osnmarket-xpress-logger
package via npm:
yarn add osnmarket-xpress-logger@latest
Or with npm
npm install osnmarket-xpress-logger@latest
import express from 'express';
import { writeLog } from 'osnmarket-xpress-logger';
const app = express();
app.use((req, res, next) => {
// Log request details
writeLog({
crypt: ['client_id', 'client_secret'], // Specify sensitive keys to replace
appName: 'your-app-name',
req,
res,
manualLog: { // this overrides the default req, res logging keys retrieval to specify your own
logFrom: req.ip,
userIp: req?.socket?.remoteAddress,
method: req.method,
payload: JSON.stringify(req.body),
headers: JSON.stringify(req.headers),
logTarget: req.originalUrl,
userAgent: req.headers['user-agent'],
logStatus: res.statusCode,
logStatusCode: res?.statusMessage?.toUpperCase(),
},
logLevel: 'INFO', // default log level
action: 'INCOMING REQUEST'
logConsole: false // it displays logs directly on console instead of writing it on file
constantFileName: false // is at default false but can be set to true if you want the same file name all the time
});
next();
});
// Your Express app routes...
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Which something like this
{ "log.level":"info", "@timestamp": "2024-03-05T09:59:41.230Z", "event.type": "AUDIT", "client.ip": "::1", "source.name": "::1", "user.name": "anonymousUser", "event.category": "INFO", "event.action": "INCOMING REQUEST", "http.request.method": "POST", "http.request.body.content": "{\"client_id\":\"***************\",\"client_secret\":\"***************\"}", "http.request.headers": "{\"content-type\":\"application/json\",\"user-agent\":\"PostmanRuntime/7.36.3\",\"accept\":\"*/*\",\"postman-token\":\"2596c2e1-23a1-4f67-af20-a8e406dde134\",\"host\":\"localhost:3000\",\"accept-encoding\":\"gzip, deflate, br\",\"connection\":\"keep-alive\",\"content-length\":\"98\"}", "url.path": "/auth/v1/token", "user_agent.original": "PostmanRuntime/7.36.3", "process.thread.id": 47575, "http.response.status": "OK", "http.response.status_code": 200 }
When in dev enviroment the logs format change to this
log.level:info|@timestamp:2024-03-05T10:01:24.432Z|event.type:AUDIT|client.ip:::1|source.name:::1|user.name:anonymousUser|event.category:INFO|event.action:INCOMING REQUEST|http.request.method:POST|http.request.body.content:"{\"client_id\":\"***************\",\"client_secret\":\"***************\"}"|http.request.headers:"{\"content-type\":\"application/json\",\"user-agent\":\"PostmanRuntime/7.36.3\",\"accept\":\"*/*\",\"postman-token\":\"c1e0ac21-61f0-4258-9142-34e8c677d24f\",\"host\":\"localhost:3000\",\"accept-encoding\":\"gzip, deflate, br\",\"connection\":\"keep-alive\",\"content-length\":\"98\"}"|url.path:/auth/v1/token|user_agent.original:PostmanRuntime/7.36.3|process.thread.id:48111|http.response.status:OK|http.response.status_code:200
-
crypt
: An array of sensitive keys to replace their values with asterisks in logs. -
appName
: Name of your application. -
req
: Entry req entity. -
res
: Outgoing res entity. -
manualLog
: Your own keys entity.-
logFrom
: IP address of the requester. -
userIp
: IP address of the user. -
method
: HTTP method. -
payload
: Request body payload. -
headers
: Request headers. -
logTarget
: Requested URL. -
userAgent
: User agent. -
logStatus
: HTTP response status code. -
logStatusCode
: HTTP response status message.
-
-
logLevel
: Log level. -
action
: Action being logged. -
logConsole
: Displays logs directly on console instead of writing it on file. -
constantFileName
: Keeps the same filename all the time.