Skip to content

Commit

Permalink
feat(logger): custom message key (#1546)
Browse files Browse the repository at this point in the history
  • Loading branch information
JacopoDaeli committed Jun 1, 2021
1 parent d8a3186 commit 01c2006
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/configuration.md
Expand Up @@ -25,6 +25,7 @@ Some less common environment variables are:
| `LOG_FORMAT` | - | By default, logs are formatted for readability in development. You can set this to `json` in order to disable the formatting |
| `LOG_LEVEL` | `const log = require('pino')({ level })` | The verbosity of logs to show when running your app, which can be `fatal`, `error`, `warn`, `info`, `debug`, `trace` or `silent`. Default: `info` |
| `LOG_LEVEL_IN_STRING` | - | By default, when using the `json` format, the level printed in the log records is an int (`10`, `20`, ..). This option tells the logger to print level as a string: `{"level": "info"}`. Default `false` |
| `LOG_MESSAGE_KEY` | `const log = require('pino')({ messageKey: 'msg' })` | Only relevant when `LOG_FORMAT` is set to `json`. Sets the json key for the log message. Default: `msg` |
| `SENTRY_DSN` | - | Set to a [Sentry](https://sentry.io/) DSN to report all errors thrown by your app. <p>_(Example: `https://1234abcd@sentry.io/12345`)_</p> |
| `PORT` | `new Server({ host })` | The port to start the local server on. Default: `3000` |
| `HOST` | `new Server({ host })` | The host to start the local server on. |
Expand Down
6 changes: 6 additions & 0 deletions src/bin/probot-receive.ts
Expand Up @@ -50,6 +50,11 @@ async function main() {
"Set to log levels (trace, debug, info, ...) as words instead of numbers (10, 20, 30, ...)",
process.env.LOG_LEVEL_IN_STRING === "true"
)
.option(
"--log-message-key",
"Set to the string key for the 'message' in the log JSON object",
process.env.LOG_MESSAGE_KEY || "msg"
)
.option(
"--sentry-dsn <dsn>",
'Set to your Sentry DSN, e.g. "https://1234abcd@sentry.io/12345"',
Expand All @@ -75,6 +80,7 @@ async function main() {
level: program.logLevel,
logFormat: program.logFormat,
logLevelInString: program.logLevelInString,
logMessageKey: program.logMessageKey,
sentryDsn: program.sentryDsn,
});

Expand Down
1 change: 1 addition & 0 deletions src/bin/read-env-options.ts
Expand Up @@ -18,6 +18,7 @@ export function readEnvOptions(
logLevel: env.LOG_LEVEL as LogLevel,
logFormat: env.LOG_FORMAT as PinoOptions["logFormat"],
logLevelInString: env.LOG_LEVEL_IN_STRING === "true",
logMessageKey: env.LOG_MESSAGE_KEY,
sentryDsn: env.SENTRY_DSN,
redisConfig: env.REDIS_URL,
baseUrl: env.GHE_HOST
Expand Down
2 changes: 2 additions & 0 deletions src/create-probot.ts
Expand Up @@ -19,6 +19,7 @@ const DEFAULTS = {
LOG_FORMAT: "",
LOG_LEVEL: "warn",
LOG_LEVEL_IN_STRING: "",
LOG_MESSAGE_KEY: "msg",
REDIS_URL: "",
SENTRY_DSN: "",
};
Expand Down Expand Up @@ -63,6 +64,7 @@ export function createProbot({
level: probotOptions.logLevel,
logFormat: envWithDefaults.LOG_FORMAT as PinoOptions["logFormat"],
logLevelInString: envWithDefaults.LOG_LEVEL_IN_STRING === "true",
logMessageKey: envWithDefaults.LOG_MESSAGE_KEY,
sentryDsn: envWithDefaults.SENTRY_DSN,
};

Expand Down
13 changes: 10 additions & 3 deletions src/helpers/get-log.ts
Expand Up @@ -17,12 +17,19 @@
import pino, { LoggerOptions } from "pino";
import { getTransformStream, Options, LogLevel } from "@probot/pino";

export type GetLogOptions = { level?: LogLevel } & Options;
export type GetLogOptions = {
level?: LogLevel;
logMessageKey?: string;
} & Options;

export function getLog(options: GetLogOptions = {}) {
const { level, ...getTransformStreamOptions } = options;
const { level, logMessageKey, ...getTransformStreamOptions } = options;

const pinoOptions: LoggerOptions = { level: level || "info", name: "probot" };
const pinoOptions: LoggerOptions = {
level: level || "info",
name: "probot",
messageKey: logMessageKey || "msg",
};
const transform = getTransformStream(getTransformStreamOptions);
// @ts-ignore TODO: check out what's wrong here
transform.pipe(pino.destination(1));
Expand Down
3 changes: 2 additions & 1 deletion src/probot.ts
Expand Up @@ -50,8 +50,9 @@ export class Probot {
options.secret = options.secret || "development";

let level = options.logLevel;
const logMessageKey = options.logMessageKey;

this.log = aliasLog(options.log || getLog({ level }));
this.log = aliasLog(options.log || getLog({ level, logMessageKey }));

// TODO: support redis backend for access token cache if `options.redisConfig`
const cache = new LRUCache<number, string>({
Expand Down
2 changes: 2 additions & 0 deletions src/run.ts
Expand Up @@ -34,6 +34,7 @@ export async function run(
logLevel: level,
logFormat,
logLevelInString,
logMessageKey,
sentryDsn,

// server options
Expand All @@ -57,6 +58,7 @@ export async function run(
level,
logFormat,
logLevelInString,
logMessageKey,
sentryDsn,
};

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Expand Up @@ -20,6 +20,7 @@ export interface Options {
secret?: string;
webhookPath?: string;
logLevel?: "trace" | "debug" | "info" | "warn" | "error" | "fatal";
logMessageKey?: string;
port?: number;
host?: string;
baseUrl?: string;
Expand Down
28 changes: 28 additions & 0 deletions test/create-probot.test.ts
@@ -1,3 +1,4 @@
import SonicBoom from "sonic-boom";
import { createProbot, Probot } from "../src";

const env = {
Expand Down Expand Up @@ -62,4 +63,31 @@ describe("createProbot", () => {
});
expect(probot.log.level).toEqual("trace");
});

test("env, logger message key", () => {
let outputData = "";

const sbWrite = SonicBoom.prototype.write;
SonicBoom.prototype.write = function (data) {
outputData += data;
};

const probot = createProbot({
env: {
...env,
LOG_LEVEL: "info",
LOG_FORMAT: "json",
LOG_MESSAGE_KEY: "myMessage",
},
defaults: { logLevel: "trace" },
});

probot.log.info("Ciao");

try {
expect(JSON.parse(outputData).myMessage).toEqual("Ciao");
} finally {
SonicBoom.prototype.write = sbWrite;
}
});
});

0 comments on commit 01c2006

Please sign in to comment.