logger: make imports esm compatible

This commit is contained in:
Abdullah Atta
2024-09-23 12:23:17 +05:00
parent e0beef745e
commit 9b091f0bb0
3 changed files with 16 additions and 14 deletions

View File

@@ -17,8 +17,8 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { consoleReporter } from "./reporters/console"; import { consoleReporter } from "./reporters/console.js";
import { ILogReporter, LoggerConfig, LogLevel } from "./types"; import { ILogReporter, LoggerConfig, LogLevel } from "./types.js";
type LogLevelFunc = (message: string, extras?: Record<string, unknown>) => void; type LogLevelFunc = (message: string, extras?: Record<string, unknown>) => void;
type ErrorLogLevelFunc = ( type ErrorLogLevelFunc = (
@@ -48,12 +48,12 @@ export class Logger implements ILogger {
scope(scope: string) { scope(scope: string) {
return new Logger({ ...this.config, scope }); return new Logger({ ...this.config, scope });
} }
fatal = errorLogLevelFactory(LogLevel.Fatal, this.config); fatal = errorLogLevelFactory(LogLevel.Fatal, () => this.config);
warn = logLevelFactory(LogLevel.Warn, this.config); warn = logLevelFactory(LogLevel.Warn, () => this.config);
debug = logLevelFactory(LogLevel.Debug, this.config); debug = logLevelFactory(LogLevel.Debug, () => this.config);
error = errorLogLevelFactory(LogLevel.Error, this.config); error = errorLogLevelFactory(LogLevel.Error, () => this.config);
info = logLevelFactory(LogLevel.Info, this.config); info = logLevelFactory(LogLevel.Info, () => this.config);
log = logLevelFactory(LogLevel.Log, this.config); log = logLevelFactory(LogLevel.Log, () => this.config);
measure(tag: string) { measure(tag: string) {
performance.mark(tag); performance.mark(tag);
@@ -84,12 +84,13 @@ export class NoopLogger implements ILogger {
} }
} }
export * from "./types"; export * from "./types.js";
export * from "./reporters"; export * from "./reporters/index.js";
function logLevelFactory(level: LogLevel, config: LoggerConfig) { function logLevelFactory(level: LogLevel, getConfig: () => LoggerConfig) {
return (message: string, extras?: Record<string, unknown>) => { return (message: string, extras?: Record<string, unknown>) => {
const now = Date.now(); const now = Date.now();
const config = getConfig();
config.reporter.write({ config.reporter.write({
level, level,
message, message,
@@ -102,13 +103,14 @@ function logLevelFactory(level: LogLevel, config: LoggerConfig) {
}; };
} }
function errorLogLevelFactory(level: LogLevel, config: LoggerConfig) { function errorLogLevelFactory(level: LogLevel, getConfig: () => LoggerConfig) {
return ( return (
error: Error | unknown, error: Error | unknown,
fallbackMessage?: string, fallbackMessage?: string,
extras?: Record<string, unknown> extras?: Record<string, unknown>
) => { ) => {
const now = Date.now(); const now = Date.now();
const config = getConfig();
config.reporter.write({ config.reporter.write({
level, level,
message: message:

View File

@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import { ILogReporter, LogLevel, LogMessage } from "../types"; import { ILogReporter, LogLevel, LogMessage } from "../types.js";
export const consoleReporter: ILogReporter = { export const consoleReporter: ILogReporter = {
write: (log) => { write: (log) => {

View File

@@ -17,4 +17,4 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
export * from "./console"; export * from "./console.js";