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: add fileTransport #1189

Merged
merged 10 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ yarn-error.log
/.clinic
**.log
**/logs

# Yarn berry
.yarn/*
!.yarn/releases
!.yarn/plugins
Expand Down
2 changes: 2 additions & 0 deletions src/SidecarConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export class SidecarConfig {
JSON: config.Get(MODULES.LOG, CONFIG.JSON) as boolean,
FILTER_RPC: config.Get(MODULES.LOG, CONFIG.FILTER_RPC) as boolean,
STRIP_ANSI: config.Get(MODULES.LOG, CONFIG.STRIP_ANSI) as boolean,
WRITE: config.Get(MODULES.LOG, CONFIG.WRITE) as boolean,
WRITE_PATH: config.Get(MODULES.LOG, CONFIG.WRITE_PATH) as string,
},
};

Expand Down
28 changes: 28 additions & 0 deletions src/Specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,33 @@ export class Specs {
}
)
);

this._specs.appendSpec(
MODULES.LOG,
this._specs.getSpec(
CONFIG.WRITE,
'Whether or not to write the logs locally',
{
default: 'false',
type: 'boolean',
regexp: /^true|false$/,
mandatory: false,
}
)
);

this._specs.appendSpec(
MODULES.LOG,
this._specs.getSpec(
CONFIG.WRITE_PATH,
'If WRITE is true, the path to write the logs too.',
{
// TODO: Need <ROOT> of this directory
default: `${__dirname}/logs`,
type: 'string',
mandatory: false,
}
)
);
}
}
23 changes: 19 additions & 4 deletions src/logging/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,39 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

import { createLogger, Logger } from 'winston';
import { ConsoleTransportInstance } from 'winston/lib/winston/transports';
import {
ConsoleTransportInstance,
FileTransportInstance,
} from 'winston/lib/winston/transports';

import { consoleTransport } from './transports';
import { SidecarConfig } from '../SidecarConfig';
import { consoleTransport, fileTransport } from './transports';

/**
* Access a singleton winston.Logger that will be intialized on first use.
*/
export class Log {
private static _transports: ConsoleTransportInstance[] | undefined;
private static _transports:
| (ConsoleTransportInstance | FileTransportInstance)[]
| undefined;
private static _logger: Logger | undefined;
private static create(): Logger {
if (this._logger) {
return this._logger;
}

// Note: there is a `fileTransport` that gets added in main.
this._transports = [consoleTransport()];

/**
* By default this will be false unless specified as an ENV var.
*/
if (SidecarConfig.config.LOG.WRITE) {
this._transports.push(
fileTransport('error', 'errors.log'),
fileTransport('info', 'logs.log')
);
}

this._logger = createLogger({
transports: this._transports,
exitOnError: false,
Expand Down
16 changes: 16 additions & 0 deletions src/logging/transports/fileTransport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { transports } from 'winston';

import { SidecarConfig } from '../../SidecarConfig';

export const fileTransport = (
level: string,
fileName: string
): transports.FileTransportInstance => {
return new transports.File({
level,
filename: `${SidecarConfig.config.LOG.WRITE_PATH}/${fileName}`,
handleExceptions: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
});
};
1 change: 1 addition & 0 deletions src/logging/transports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

export * from './consoleTransport';
export * from './fileTransport';
2 changes: 2 additions & 0 deletions src/types/sidecar-config/CONFIG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ export enum CONFIG {
TYPES_CHAIN = 'TYPES_CHAIN',
TYPES_SPEC = 'TYPES_SPEC',
TYPES = 'TYPES',
WRITE = 'WRITE',
WRITE_PATH = 'WRITE_PATH',
}
2 changes: 2 additions & 0 deletions src/types/sidecar-config/SidecarConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ interface ISidecarConfigLog {
JSON: boolean;
FILTER_RPC: boolean;
STRIP_ANSI: boolean;
WRITE: boolean;
WRITE_PATH: string;
}