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

Replace Winston Logger with Pino #506

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 12 additions & 1 deletion packages/usdk/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ const logger = LoggerFactory.getLogger();
};
});

// Set up global error handling
['uncaughtException', 'unhandledRejection'].forEach(event =>
process.on(event, (err, err2) => {
console.error('cli uncaught exception', err, err2);
console.log("A complete log of this run can be found in: \n", logger.getLogFilePath());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea

process.exit(1);
})
);


//

const makeSupabase = (jwt) => makeAnonymousClient(env, jwt);
Expand Down Expand Up @@ -1373,7 +1383,8 @@ const handleError = async (fn) => {
return await fn();
} catch (err) {
console.warn(err.stack);
process.exit(1);
console.log("A complete log of this run can be found in: \n", logger.getLogFilePath());
// process.exit(1);
}
};
export const main = async () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/usdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
"openai": "^4.56.0",
"ora": "^8.1.1",
"picocolors": "^1.0.1",
"pino": "^9.5.0",
"pino-pretty": "^13.0.0",
"pretty-bytes": "^6.1.1",
"queue-manager": "file:./packages/upstreet-agent/packages/queue-manager",
"react-agents-wrangler": "file:./packages/upstreet-agent/packages/react-agents-wrangler",
Expand Down
8 changes: 0 additions & 8 deletions packages/usdk/usdk.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#!/usr/bin/env -S node --no-warnings --experimental-wasm-modules
import { main } from './cli.js';

// Set up global error handling
['uncaughtException', 'unhandledRejection'].forEach(event =>
process.on(event, (err, err2) => {
console.log('cli uncaught exception', err, err2);
process.exit(1);
})
);

// Execute CLI
(async () => {
try {
Expand Down
3 changes: 2 additions & 1 deletion packages/usdk/util/logger/logger-factory.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import PinoLogger from "./logger-pino.mjs";
import WinstonLogger
from "./logger-winston.mjs";

class LoggerFactory {
static getLogger() {
return new WinstonLogger();
return new PinoLogger();
}
}

Expand Down
76 changes: 76 additions & 0 deletions packages/usdk/util/logger/logger-pino.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// pino-logger.mjs
import pino from 'pino';
import { getLogDirectory } from '../path/index.mjs';
import path from 'path';
import { Logger } from './logger-interface.mjs';


class PinoLogger extends Logger{
static instance = null;

constructor() {
super();
if (PinoLogger.instance) {
return PinoLogger.instance;
}
const currentDateTime = new Date().toISOString().replace(/[:.]/g, '-');
const currentModuleDir = getLogDirectory();
this.logFilePath = path.join(currentModuleDir, `log-${currentDateTime}.log`);

const transport = pino.transport({
targets: [
{
target: 'pino-pretty',
options: {
destination: this.logFilePath,
colorize: false,
translateTime: 'SYS:standard',
ignore: 'pid,hostname',
singleLine: false,
}
}
],
});

this.logger = pino({
level: process.env.LOG_LEVEL || 'info',
timestamp: pino.stdTimeFunctions.isoTime,
formatters: {
level: (label) => {
return { level: label.toUpperCase() };
}
},
}, transport);
PinoLogger.instance = this;
}

info(message, ...args) {
this.logger.info(message, ...args);
}

error(message, error = null) {
if (error) {
this.logger.error({ err: error }, message);
} else {
this.logger.error(message);
}
}

warn(message, ...args) {
this.logger.warn(message, ...args);
}

debug(message, ...args) {
this.logger.debug(message, ...args);
}

getChildLogger(bindings) {
return this.logger.child(bindings);
}

getLogFilePath() {
return this.logFilePath;
}
}

export default PinoLogger;
8 changes: 6 additions & 2 deletions packages/usdk/util/logger/logger-winston.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class WinstonLogger extends Logger {
super();
const currentDateTime = new Date().toISOString().replace(/[:.]/g, '-');
const currentModuleDir = getLogDirectory();
const logFilePath = path.join(currentModuleDir, `log-${currentDateTime}.log`);
this.logFilePath = path.join(currentModuleDir, `log-${currentDateTime}.log`);

this.logger = winston.createLogger({
level: 'error',
Expand All @@ -19,7 +19,7 @@ class WinstonLogger extends Logger {
})
),
transports: [
new winston.transports.File({ filename: logFilePath, name: 'error-file', level: 'info'}),
new winston.transports.File({ filename: this.logFilePath, name: 'error-file', level: 'info'}),
new winston.transports.Console()
]
});
Expand All @@ -38,6 +38,10 @@ class WinstonLogger extends Logger {
error(...args) {
this.logger.error(...args);
}

getLogFilePath() {
return this.logFilePath;
}
}

export default WinstonLogger;
Loading