SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.
JavaScript a2aagentic-aiai-agentsdevtoolsmcp-clientn8n-alternativeno-codeopen-sourceorchestrationsmythosvisual-builder
/* eslint-disable no-useless-constructor */
/* eslint-disable max-classes-per-file */
import winston from 'winston';
import Transport from 'winston-transport';
winston.addColors({
error: 'red',
warn: 'yellow',
info: 'green',
debug: 'blue',
});
// Retrieve the DEBUG environment variable and split it into an array of namespaces
const namespaces = (process.env.LOG_FILTER || '').split(',');
// Create a Winston format that filters messages based on namespaces
const namespaceFilter = winston.format(info => {
// If DEBUG is not set, log everything
if (!process.env.LOG_FILTER || namespaces.some(ns => (info.module as string)?.includes(ns))) {
return info;
}
return false; // Filter out messages that do not match the namespace
})();
// Custom stream for your transport
class ArrayTransport extends Transport {
private logs: any[];
constructor(opts: winston.transport.TransportStreamOptions | undefined) {
super(opts);
// Configure your storage array
this.logs = (opts as any)?.logs || [];
}
log(info: { level: any; message: any }, callback: () => void) {
setImmediate(() => {
this.emit('logged', info);
});
// Perform the writing to the array storage
this.logs.push(`${info.level}: ${info.message}`);
// Perform the writing to the remote service
callback();
}
}
class Logger {
public startTime = Date.now();
public get output() {
return Array.isArray(this.data) ? this.data.join('\n') : undefined;
}
public get elapsedTime() {
return Date.now() - this.startTime;
}
constructor(private _logger: winston.Logger, public data: any[] | undefined, private labels: { [key: string]: any }) {}
public log(...args: any[]) {
this._logger.log('info', formatLogMessage(...args), this.labels);
}
public warn(...args: any[]) {
this._logger.log('warn', formatLogMessage(...args), this.labels);
}
public debug(...args: any[]) {
this._logger.log('debug', formatLogMessage(...args), this.labels);
}
public info(...args: any[]) {
this._logger.log('info', formatLogMessage(...args), this.labels);
}
public verbose(...args: any[]) {
this._logger.log('verbose', formatLogMessage(...args), this.labels);
}
public error(...args: any[]) {
const stack = new Error().stack;
this._logger.log('error', formatLogMessage(...args), { ...this.labels, stack });
}
}
const MAX_LOG_MESSAGE_LENGTH = 500;
function createBaseLogger(memoryStore?: any[]) {
const logger = winston.createLogger({
// level: 'info', // log level
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({
stack: true,
}),
winston.format.splat(),
winston.format.json(),
),
transports: [
new winston.transports.Console({
level: 'error',
// handleExceptions: true,
format: winston.format.combine(
winston.format.printf(info => {
let message = info.message;
message =
(message as string)?.length > MAX_LOG_MESSAGE_LENGTH ? `${(message as string).substring(0, MAX_LOG_MESSAGE_LENGTH)}...` : message;
return `${info.level}:${info.module || ''} ${message} ${info.stack || ''}`;
}),
),
stderrLevels: ['error'], // Define levels that should be logged to stderr
}),
new winston.transports.Console({
level: process.env.LOG_LEVEL || 'info',
format: winston.format.combine(
namespaceFilter,
winston.format.printf(info => {
const module = info.module ? winston.format.colorize().colorize(info.level, ` [${info.module}]`) : '';
const ns = winston.format.colorize().colorize(info.level, `${info.level}${module}`);
let message = info.message;
message =
(message as string)?.length > MAX_LOG_MESSAGE_LENGTH ? `${(message as string).substring(0, MAX_LOG_MESSAGE_LENGTH)}...` : message;
return `${ns} - ${message}`;
}),
),
// handleExceptions: true,
}),
],
});
if (Array.isArray(memoryStore)) {
logger.add(
new ArrayTransport({
level: 'debug',
// @ts-ignore
logs: memoryStore,
}),
);
}
return logger;
}
function formatLogMessage(...args: any[]) {
return args
.map(arg => {
// If the argument is an object (and not null), serialize it to JSON
if (typeof arg === 'object' && arg !== null && !(arg instanceof Error)) {
return JSON.stringify(arg, null, 2); // set the space to 2 for better readability
}
// Otherwise, just convert it to a string in case it's not
return String(arg);
})
.join(' '); // Concatenate all arguments with a space
}
function createLabeledLogger(labels: { [key: string]: any }, memoryStore?: any[]) {
const _logger = createBaseLogger(memoryStore);
_logger.defaultMeta = labels;
const logger = new Logger(_logger, memoryStore, labels);
return logger;
}
export function createLogger(module: string, withMemoryStore = false) {
return createLabeledLogger({ module }, withMemoryStore ? [] : undefined);
}