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 9 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
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,17 @@ export SAS_SUBSTRATE_TYPES=/path/to/my-chains-types.json

### Logging

- `SAS_LOG_LEVEL`: the lowest priority log level to surface, defaults to `info`. Tip: set to `http`
- `SAS_LOG_LEVEL`: The lowest priority log level to surface, defaults to `info`. Tip: set to `http`
to see all HTTP requests.
- `SAS_LOG_JSON`: wether or not to have logs formatted as JSON, defaults to `false`.
- `SAS_LOG_JSON`:Whether or not to have logs formatted as JSON, defaults to `false`.
Useful when using `stdout` to programmatically process Sidecar log data.
- `SAS_LOG_FILTER_RPC`: wether or not to filter polkadot-js API-WS RPC logging, defaults to `false`.
- `SAS_LOG_STRIP_ANSI`: wether or not to strip ANSI characters from logs, defaults
- `SAS_LOG_FILTER_RPC`: Whether or not to filter polkadot-js API-WS RPC logging, defaults to `false`.
- `SAS_LOG_STRIP_ANSI`: Whether or not to strip ANSI characters from logs, defaults
to `false`. Useful when logging RPC calls with JSON written to transports.
- `SAS_LOG_WRITE`: Whether or not to write logs to a log file. Default is set to `false`. Accepts a boolean value. The log files will be written as `logs.log`. **NOTE**: Tt will only log what is available depending on what `SAS_LOG_LEVEL` is set to.
- `SAS_LOG_WRITE_PATH`: Specifies the path to write the log files. Default will be where the package is installed.
- `SAS_LOG_WRITE_MAX_FILE_SIZE`: Specifies in bytes what the max file size for the written log files should be. Default is `5242880` (5MB).
- `SAS_LOG_WRITE_MAX_FILES`: Specifies how many files can be written. Default is 5.

#### Log levels

Expand Down
10 changes: 10 additions & 0 deletions src/SidecarConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ 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,
WRITE_MAX_FILE_SIZE: config.Get(
MODULES.LOG,
CONFIG.WRITE_MAX_FILE_SIZE
) as number,
WRITE_MAX_FILES: config.Get(
MODULES.LOG,
CONFIG.WRITE_MAX_FILES
) as number,
},
};

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

// WRITE
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,
}
)
);

// WRITE_PATH
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,
}
)
);

// WRITE_MAX_FILE_SIZE
this._specs.appendSpec(
MODULES.LOG,
this._specs.getSpec(
CONFIG.WRITE_MAX_FILE_SIZE,
'The max size the log file should not exceed.',
{
default: 5242880, // 5MB
type: 'number',
mandatory: false,
}
)
);

// WRITE_MAX_FILES
this._specs.appendSpec(
MODULES.LOG,
this._specs.getSpec(
CONFIG.WRITE_MAX_FILES,
'The max amount of files that should be created.',
{
default: 5,
type: 'number',
mandatory: false,
}
)
);
}
}
20 changes: 16 additions & 4 deletions src/logging/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,36 @@
// 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('logs.log'));
}

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

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

export const fileTransport = (
fileName: string
): transports.FileTransportInstance => {
return new transports.File({
level: SidecarConfig.config.LOG.LEVEL,
filename: `${SidecarConfig.config.LOG.WRITE_PATH}/${fileName}`,
handleExceptions: true,
maxsize: SidecarConfig.config.LOG.WRITE_MAX_FILE_SIZE,
maxFiles: SidecarConfig.config.LOG.WRITE_MAX_FILES,
});
};
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';
4 changes: 4 additions & 0 deletions src/types/sidecar-config/CONFIG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ export enum CONFIG {
TYPES_CHAIN = 'TYPES_CHAIN',
TYPES_SPEC = 'TYPES_SPEC',
TYPES = 'TYPES',
WRITE = 'WRITE',
WRITE_PATH = 'WRITE_PATH',
WRITE_MAX_FILE_SIZE = 'WRITE_MAX_FILE_SIZE',
WRITE_MAX_FILES = 'WRITE_MAX_FILES',
}
4 changes: 4 additions & 0 deletions src/types/sidecar-config/SidecarConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ interface ISidecarConfigLog {
JSON: boolean;
FILTER_RPC: boolean;
STRIP_ANSI: boolean;
WRITE: boolean;
WRITE_PATH: string;
WRITE_MAX_FILE_SIZE: number;
WRITE_MAX_FILES: number;
}