Skip to content

Commit

Permalink
Enable ts strict mode in core-logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikearaya committed Oct 28, 2023
1 parent 3b37da9 commit b70bd52
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
23 changes: 18 additions & 5 deletions packages/logger/src/createLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@ import { LogLevel } from './logger.types.js';
const require = createRequire(import.meta.url);
const { stringify } = require('safe-stable-stringify');

const { DEBUG = '', LOG_LEVEL = LogLevel.Info, UNCHAINED_LOG_FORMAT = 'unchained' } = process.env;
enum SupportedLogFormats {
'unchained',
'json',
}

const { combine, colorize, json } = format;
const {
DEBUG = '',
LOG_LEVEL = LogLevel.Info,
UNCHAINED_LOG_FORMAT = 'unchained',
} = process.env as unknown as {
DEBUG?: string;
LOG_LEVEL: LogLevel;
UNCHAINED_LOG_FORMAT: SupportedLogFormats;
};

const { combine, colorize, json } = format;
const logFormat = (UNCHAINED_LOG_FORMAT as string).toLowerCase();
const debugStringContainsModule = (debugString: string, moduleName: string) => {
if (!debugString) return false;
const loggingMatched = debugString.split(',').reduce((accumulator: any, name: string) => {
Expand Down Expand Up @@ -40,12 +53,12 @@ const myFormat = format.printf(({ level, message, label, timestamp, stack, ...re
.join(' ');
});

const UnchainedLogFormats = {
const UnchainedLogFormats: { [key: string]: ReturnType<typeof json | typeof combine> } = {
unchained: combine(colorize(), myFormat),
json: json(),
};

if (!UnchainedLogFormats[UNCHAINED_LOG_FORMAT.toLowerCase()]) {
if (!UnchainedLogFormats[logFormat]) {
throw new Error(
`UNCHAINED_LOG_FORMAT is invalid, use one of ${Object.keys(UnchainedLogFormats).join(',')}`,
);
Expand All @@ -63,7 +76,7 @@ export const createLogger = (moduleName: string, moreTransports: Array<Transport
),
transports: [
new transports.Console({
format: UnchainedLogFormats[UNCHAINED_LOG_FORMAT],
format: UnchainedLogFormats[logFormat],
stderrLevels: [LogLevel.Error],
consoleWarnLevels: [LogLevel.Warning],
level: loggingMatched ? LogLevel.Debug : LOG_LEVEL,
Expand Down
1 change: 1 addition & 0 deletions packages/logger/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../shared/base.tsconfig.json",
"compilerOptions": {
"strict": true,
"rootDir": "src",
"outDir": "lib",
},
Expand Down

0 comments on commit b70bd52

Please sign in to comment.