Replace bunyan with pino
This commit is contained in:
parent
b0bee86fd3
commit
d85a43fe93
7 changed files with 251 additions and 130 deletions
|
@ -8,12 +8,14 @@
|
|||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import { app, ipcMain as ipc } from 'electron';
|
||||
import * as bunyan from 'bunyan';
|
||||
import pinoms from 'pino-multi-stream';
|
||||
import pino from 'pino';
|
||||
import * as mkdirp from 'mkdirp';
|
||||
import * as _ from 'lodash';
|
||||
import readFirstLine from 'firstline';
|
||||
import { read as readLastLines } from 'read-last-lines';
|
||||
import rimraf from 'rimraf';
|
||||
import { createStream } from 'rotating-file-stream';
|
||||
|
||||
import {
|
||||
LogEntryType,
|
||||
|
@ -33,11 +35,11 @@ declare global {
|
|||
}
|
||||
}
|
||||
|
||||
let globalLogger: undefined | bunyan;
|
||||
let globalLogger: undefined | pinoms.Logger;
|
||||
|
||||
const isRunningFromConsole = Boolean(process.stdout.isTTY);
|
||||
|
||||
export async function initialize(): Promise<bunyan> {
|
||||
export async function initialize(): Promise<pinoms.Logger> {
|
||||
if (globalLogger) {
|
||||
throw new Error('Already called initialize!');
|
||||
}
|
||||
|
@ -62,26 +64,25 @@ export async function initialize(): Promise<bunyan> {
|
|||
}
|
||||
|
||||
const logFile = path.join(logPath, 'main.log');
|
||||
const loggerOptions: bunyan.LoggerOptions = {
|
||||
name: 'main',
|
||||
streams: [
|
||||
{
|
||||
type: 'rotating-file',
|
||||
path: logFile,
|
||||
period: '1d',
|
||||
count: 3,
|
||||
},
|
||||
],
|
||||
};
|
||||
const stream = createStream(logFile, {
|
||||
interval: '1d',
|
||||
maxFiles: 3,
|
||||
});
|
||||
|
||||
const streams: pinoms.Streams = [];
|
||||
streams.push({ stream });
|
||||
|
||||
if (isRunningFromConsole) {
|
||||
loggerOptions.streams?.push({
|
||||
level: 'debug',
|
||||
streams.push({
|
||||
level: 'debug' as const,
|
||||
stream: process.stdout,
|
||||
});
|
||||
}
|
||||
|
||||
const logger = bunyan.createLogger(loggerOptions);
|
||||
const logger = pinoms({
|
||||
streams,
|
||||
timestamp: pino.stdTimeFunctions.isoTime,
|
||||
});
|
||||
|
||||
ipc.on('fetch-log', event => {
|
||||
fetch(logPath).then(
|
||||
|
|
|
@ -8,7 +8,8 @@
|
|||
import { ipcRenderer as ipc } from 'electron';
|
||||
import _ from 'lodash';
|
||||
import * as path from 'path';
|
||||
import * as bunyan from 'bunyan';
|
||||
import pino from 'pino';
|
||||
import { createStream } from 'rotating-file-stream';
|
||||
|
||||
import { uploadDebugLogs } from './debuglogs';
|
||||
import { redactAll } from '../../js/modules/privacy';
|
||||
|
@ -23,7 +24,8 @@ import * as log from './log';
|
|||
import { reallyJsonStringify } from '../util/reallyJsonStringify';
|
||||
|
||||
// To make it easier to visually scan logs, we make all levels the same length
|
||||
const levelMaxLength: number = Object.keys(bunyan.levelFromName).reduce(
|
||||
const levelFromName = pino().levels.values;
|
||||
const levelMaxLength: number = Object.keys(levelFromName).reduce(
|
||||
(maxLength, level) => Math.max(maxLength, level.length),
|
||||
0
|
||||
);
|
||||
|
@ -96,7 +98,7 @@ function fetch(): Promise<string> {
|
|||
});
|
||||
}
|
||||
|
||||
let globalLogger: undefined | bunyan;
|
||||
let globalLogger: undefined | pino.Logger;
|
||||
|
||||
export function initialize(): void {
|
||||
if (globalLogger) {
|
||||
|
@ -105,19 +107,17 @@ export function initialize(): void {
|
|||
|
||||
const basePath = ipc.sendSync('get-user-data-path');
|
||||
const logFile = path.join(basePath, 'logs', 'app.log');
|
||||
const loggerOptions: bunyan.LoggerOptions = {
|
||||
name: 'app',
|
||||
streams: [
|
||||
{
|
||||
type: 'rotating-file',
|
||||
path: logFile,
|
||||
period: '1d',
|
||||
count: 3,
|
||||
},
|
||||
],
|
||||
};
|
||||
const stream = createStream(logFile, {
|
||||
interval: '1d',
|
||||
maxFiles: 3,
|
||||
});
|
||||
|
||||
globalLogger = bunyan.createLogger(loggerOptions);
|
||||
globalLogger = pino(
|
||||
{
|
||||
timestamp: pino.stdTimeFunctions.isoTime,
|
||||
},
|
||||
stream
|
||||
);
|
||||
}
|
||||
|
||||
const publish = uploadDebugLogs;
|
||||
|
@ -127,7 +127,6 @@ const publish = uploadDebugLogs;
|
|||
const env = window.getEnvironment();
|
||||
const IS_PRODUCTION = env === 'production';
|
||||
|
||||
// The Bunyan API: https://github.com/trentm/node-bunyan#log-method-api
|
||||
function logAtLevel(level: LogLevel, ...args: ReadonlyArray<unknown>): void {
|
||||
if (!IS_PRODUCTION) {
|
||||
const prefix = getLogLevelString(level)
|
||||
|
@ -138,14 +137,13 @@ function logAtLevel(level: LogLevel, ...args: ReadonlyArray<unknown>): void {
|
|||
|
||||
const levelString = getLogLevelString(level);
|
||||
const msg = cleanArgs(args);
|
||||
const time = new Date().toISOString();
|
||||
|
||||
if (!globalLogger) {
|
||||
throw new Error('Logger has not been initialized yet');
|
||||
return;
|
||||
}
|
||||
|
||||
globalLogger[levelString]({ time }, msg);
|
||||
globalLogger[levelString](msg);
|
||||
}
|
||||
|
||||
log.setLogAtLevel(logAtLevel);
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
// Copyright 2021 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import * as bunyan from 'bunyan';
|
||||
import * as pino from 'pino';
|
||||
import { redactAll } from '../../js/modules/privacy';
|
||||
import { missingCaseError } from '../util/missingCaseError';
|
||||
import { reallyJsonStringify } from '../util/reallyJsonStringify';
|
||||
|
||||
// These match [Bunyan's recommendations][0].
|
||||
// [0]: https://www.npmjs.com/package/bunyan#levels
|
||||
// These match [Pino's recommendations][0].
|
||||
// [0]: https://getpino.io/#/docs/api?id=loggerlevels-object
|
||||
export enum LogLevel {
|
||||
Fatal = 60,
|
||||
Error = 50,
|
||||
|
@ -17,8 +17,8 @@ export enum LogLevel {
|
|||
Trace = 10,
|
||||
}
|
||||
|
||||
// These match [Bunyan's core fields][1].
|
||||
// [1]: https://www.npmjs.com/package/bunyan#core-fields
|
||||
// These match [Pino's core fields][1].
|
||||
// [1]: https://getpino.io/#/?id=usage
|
||||
export type LogEntryType = {
|
||||
level: LogLevel;
|
||||
msg: string;
|
||||
|
@ -51,7 +51,7 @@ export function isLogEntry(value: unknown): value is LogEntryType {
|
|||
return typeof msg === 'string' && isLogLevel(level) && isValidTime(time);
|
||||
}
|
||||
|
||||
export function getLogLevelString(value: LogLevel): bunyan.LogLevelString {
|
||||
export function getLogLevelString(value: LogLevel): pino.Level {
|
||||
switch (value) {
|
||||
case LogLevel.Fatal:
|
||||
return 'fatal';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue