-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logger): add configurable global logger
This allows users to configure a global logger (currently implmeneted with winston) to give helpful debugging logs. We can continue to add debug logs, as well as identify spots where performance metrics may be helpful
- Loading branch information
dtfiedler
committed
Nov 9, 2023
1 parent
8da51cc
commit e6f341a
Showing
11 changed files
with
182 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Copyright (C) 2022-2023 Permanent Data Solutions, Inc. All Rights Reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import winston, { createLogger, format, transports } from 'winston'; | ||
|
||
import { TurboLogger } from '../types.js'; | ||
import { version } from '../version.js'; | ||
|
||
export class TurboWinstonLogger implements TurboLogger { | ||
protected logger: winston.Logger; | ||
constructor({ | ||
level = 'info', | ||
logFormat = 'simple', | ||
}: { | ||
level?: 'info' | 'debug' | 'error' | 'none' | undefined; | ||
logFormat?: 'simple' | 'json' | undefined; | ||
} = {}) { | ||
this.logger = createLogger({ | ||
level, | ||
defaultMeta: { client: 'turbo-sdk', version }, | ||
silent: level === 'none', | ||
format: getLogFormat(logFormat), | ||
transports: [new transports.Console()], | ||
}); | ||
} | ||
|
||
info(message: string, ...args: any[]) { | ||
Check warning on line 40 in src/common/logger.ts GitHub Actions / build (18.x, lint)
|
||
this.logger.info(message, ...args); | ||
} | ||
|
||
warn(message: string, ...args: any[]) { | ||
Check warning on line 44 in src/common/logger.ts GitHub Actions / build (18.x, lint)
|
||
this.logger.warn(message, ...args); | ||
} | ||
|
||
error(message: string, ...args: any[]) { | ||
Check warning on line 48 in src/common/logger.ts GitHub Actions / build (18.x, lint)
|
||
this.logger.error(message, ...args); | ||
} | ||
|
||
debug(message: string, ...args: any[]) { | ||
Check warning on line 52 in src/common/logger.ts GitHub Actions / build (18.x, lint)
|
||
this.logger.debug(message, ...args); | ||
} | ||
|
||
setLogLevel(level: string) { | ||
this.logger.level = level; | ||
} | ||
|
||
setLogFormat(logFormat: string) { | ||
this.logger.format = getLogFormat(logFormat); | ||
} | ||
} | ||
|
||
function getLogFormat(logFormat: string) { | ||
return format.combine( | ||
format((info) => { | ||
if (info.stack && info.level !== 'error') { | ||
delete info.stack; | ||
} | ||
return info; | ||
})(), | ||
format.errors({ stack: true }), // Ensure errors show a stack trace | ||
format.timestamp(), | ||
logFormat === 'json' ? format.json() : format.simple(), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.