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

feat: Replace Bunyan logger with Pino #129

Merged
merged 1 commit into from
Jun 5, 2024
Merged
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
89 changes: 71 additions & 18 deletions cloudrun-malware-scanner/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,77 @@
// limitations under the License.

/**
* Create a Bunyan Logger using structured logging to stdout.
* Create a Pino Logger using structured logging to stdout.
*/

const {pino} = require('pino');
const process = require('node:process');
const bunyan = require('bunyan');
const {LoggingBunyan} = require('@google-cloud/logging-bunyan');
const pkgJson = require('./package.json');

// Create logging client.
const loggingBunyan = new LoggingBunyan({
redirectToStdout: true,
projectId: process.env.PROJECT_ID,
logName: 'malware-scanner',
useMessageField: false,
});

exports.logger = bunyan.createLogger({
name: pkgJson.name,
streams: [loggingBunyan.stream('info')],
serializers: bunyan.stdSerializers,
});

// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity
const PinoLevelToSeverityLookup = new Map([
['trace', 'DEBUG'],
['debug', 'DEBUG'],
['info', 'INFO'],
['warn', 'WARNING'],
['error', 'ERROR'],
['fatal', 'CRITICAL'],
]);

/**
* Return a pino log level based on environment variables.
*
* @return {string}
*/
function getLogLevel() {
const envLogLevel = process.env.LOG_LEVEL
? process.env.LOG_LEVEL.toLowerCase()
: null;

if (envLogLevel && PinoLevelToSeverityLookup.get(envLogLevel)) {
return envLogLevel;
} else if (process.env.NODE_ENV?.toLowerCase() === 'test') {
return 'fatal';
} else {
return 'info';
}
}

/**
* Convert pino log level to Google severity
* @param {string} label
* @param {number} number
* @returns {Object}
*/
function pinoLevelToStackdriver(label, number) {
return {
severity: PinoLevelToSeverityLookup.get(label) || 'INFO',
level: number,
};
}

/**
* Create a JSON fragment string containing the timestamp in Stackdriver format:
* <pre>
* "timestamp": {
* "seconds": nnnnn,
* "nanos": nnnnn
* }`
* </pre>
* @returns {string}
*/
function getStackdriverTimestamp() {
const millis = Date.now();
return `,"timestamp":{"seconds":${Math.floor(millis / 1000)},"nanos": ${millis % 1000}000000}`;
}

/** @type {pino.LoggerOptions} */
const pinoConfig = {
level: getLogLevel(),
messageKey: 'message',
formatters: {
level: pinoLevelToStackdriver,
},
timestamp: getStackdriverTimestamp,
};

exports.logger = pino(pinoConfig);
Loading