Skip to content

Commit

Permalink
use of TransformableInfo type & casting to string
Browse files Browse the repository at this point in the history
  • Loading branch information
Imod7 committed Nov 11, 2024
1 parent 7ce4b22 commit 624a666
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 71 deletions.
13 changes: 6 additions & 7 deletions src/logging/transformers/filterApiRpc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// Copyright 2017-2024 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
Expand All @@ -14,19 +14,18 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { TransformableInfo } from 'logform';
import { format } from 'winston';

import { ITransformableInfo } from '../../types/logging';

/**
* Ignore log messages that have `API-WS:`. (e.g. polkadot-js RPC logging)
*/
export const filterApiRpc = format((info: ITransformableInfo, _opts: unknown) => {
export const filterApiRpc = format((info: TransformableInfo) => {
if (
!info ||
(info?.message?.includes &&
!info?.message?.includes('connected') &&
info.message?.includes('API-WS:') &&
((info?.message as string)?.includes &&
!(info?.message as string)?.includes('connected') &&
(info?.message as string)?.includes('API-WS:') &&
info.level === 'info')
) {
return false;
Expand Down
11 changes: 5 additions & 6 deletions src/logging/transformers/nodeUtilFormat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// Copyright 2017-2024 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
Expand All @@ -14,22 +14,21 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { TransformableInfo } from 'logform';
import { SPLAT } from 'triple-beam';
import { format } from 'util';
import * as winston from 'winston';

import { ITransformableInfo } from '../../types/logging';

/**
* Console.log style formatting using node's `util.format`. We need this so we
* can override console.{log, error, etc.} without issue.
*/
export const nodeUtilFormat = winston.format((info: ITransformableInfo, _opts: unknown) => {
export const nodeUtilFormat = winston.format((info: TransformableInfo) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const args = info[SPLAT as unknown as string];
const args = info[SPLAT as unknown as string] as string[];
if (args) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
info.message = format(info.message, ...args);
info.message = format(info.message as string, ...args);
}
return info;
});
5 changes: 2 additions & 3 deletions src/logging/transformers/stripAnsi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { TransformableInfo } from 'logform';
import { format } from 'winston';

import { ITransformableInfo } from '../../types/logging';

/**
* Regex pattern to match ANSI characters.
*/
Expand Down Expand Up @@ -65,7 +64,7 @@ function stripAnsiShellCodes(data: unknown): unknown {
/**
* Strip ANSI characters from `TransformableInfo.message`.
*/
export const stripAnsi = format((info: ITransformableInfo, _opts: unknown) => {
export const stripAnsi = format((info: TransformableInfo) => {
info.message = stripAnsiShellCodes(info.message) as string;
return info;
});
11 changes: 5 additions & 6 deletions src/logging/transformers/stripTimestamp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// Copyright 2017-2024 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
Expand All @@ -14,10 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { TransformableInfo } from 'logform';
import { format } from 'winston';

import { ITransformableInfo } from '../../types/logging';

/**
* Regex that matches timestamps with the format of `YYYY-MM-DD HH:MM`
*/
Expand All @@ -27,9 +26,9 @@ const timestampRegex = /[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0
* Slice out the timestamp from a message so it is not redundant with the winston
* timestamp. This is for the polkadot-js console statements.
*/
export const stripTimestamp = format((info: ITransformableInfo, _opts: unknown) => {
if (timestampRegex.exec(info?.message)) {
info.message = info.message.slice(24).trim();
export const stripTimestamp = format((info: TransformableInfo) => {
if (timestampRegex.exec(info?.message as string)) {
info.message = (info.message as string).slice(24).trim();
}

return info;
Expand Down
12 changes: 6 additions & 6 deletions src/logging/transports/consoleTransport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2022 Parity Technologies (UK) Ltd.
// Copyright 2017-2024 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
Expand All @@ -14,10 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { TransformableInfo } from 'logform';
import { format, transports } from 'winston';

import { SidecarConfig } from '../../SidecarConfig';
import { ITransformableInfo } from '../../types/logging';
import { filterApiRpc, nodeUtilFormat, stripAnsi, stripTimestamp, timeStamp } from '../transformers';

/**
Expand All @@ -28,15 +28,15 @@ export function consoleTransport(): transports.ConsoleTransportInstance {
config: { LOG },
} = SidecarConfig;
/**
* A simple printing format for how `ITransformableInfo` shows up.
* A simple printing format for how `TransformableInfo` shows up.
*/
const simplePrint = format.printf((info: ITransformableInfo) => {
const simplePrint = format.printf((info: TransformableInfo) => {
if (info?.stack) {
// If there is a stack dump (e.g. error middleware), show that in console
return `${info?.timestamp} ${info?.level}: ${info?.message} \n ${info?.stack}`;
return `${info?.timestamp as string} ${info?.level}: ${info?.message as string} \n ${info?.stack as string}`;
}

return `${info?.timestamp} ${info?.level}: ${info?.message}`;
return `${info?.timestamp as string} ${info?.level}: ${info?.message as string}`;
});

const transformers = [stripTimestamp(), nodeUtilFormat(), timeStamp];
Expand Down
26 changes: 0 additions & 26 deletions src/types/logging/Transformers.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/types/logging/index.ts

This file was deleted.

0 comments on commit 624a666

Please sign in to comment.