Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional parameters for the middleware #14

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions bufflog.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Options } from "pino-http";

const pinoLogger = require('pino')({
level: process.env.LOG_LEVEL ? String.prototype.toLowerCase.apply(process.env.LOG_LEVEL) : "notice",
// probably we want to call it `msg`. if so, let's change the PHP library instead
Expand Down Expand Up @@ -49,19 +51,22 @@ export function critical(message: string, context?: object) {
pinoLogger.fatal({context: context}, message);
}

export function middleware() {
export function middleware(options?: Options) {

const { logger, genReqId, useLevel, stream, autoLogging, customLogLevel } : Options = options || {};

return require('pino-http')({
logger: pinoLogger,
logger: logger || pinoLogger,

// Define a custom logger level
customLogLevel: function (res: any, err: any) {
if (res.statusCode >= 400 && res.statusCode < 500) {
// for now, we don't want notice notification on the 4xx
// Define a custom logger level
customLogLevel: customLogLevel || function (res: any, err: any) {
if (res.statusCode >= 400 && res.statusCode < 500) {
// for now, we don't want notice notification on the 4xx
return 'info'
} else if (res.statusCode >= 500 || err) {
return 'error'
}
return 'info'
} else if (res.statusCode >= 500 || err) {
return 'error'
}
return 'info'
},
},
})
}
6 changes: 5 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ BuffLog.critical('hello critical', {"some":"stuff"});

const app = express();

app.use(BuffLog.middleware())
app.use(BuffLog.middleware())

// use custom parameter , here a pino logger
app.use(BuffLog.middleware({'logger': require('pino')()}))


app.listen(4000, () => {
console.log(`Server is listening on port 4000`);
Expand Down
10 changes: 9 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bufferapp/bufflog",
"version": "0.0.5",
"version": "0.0.6",
"description": "logger for all javascript and typescript Buffer services",
"main": "dist/bufflog.js",
"scripts": {
Expand All @@ -24,6 +24,7 @@
},
"dependencies": {
"@types/pino": "^5.15.5",
"@types/pino-http": "^4.4.0",
"dd-trace": "^0.18.0",
"pino": "^5.16.0",
"pino-http": "^5.0.0"
Expand Down