|
@@ -1,4 +1,4 @@
|
|
|
-// METADATA // {"ai-commented":{"service":"mistral","model":"mistral-large-latest"}}
|
|
|
+// METADATA // {"ai-commented":{"service":"xai"}}
|
|
|
/*
|
|
|
* Copyright (C) 2024 Puter Technologies Inc.
|
|
|
*
|
|
@@ -17,12 +17,7 @@
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
*/
|
|
|
-// Defines a function to create log severity objects used for logging
|
|
|
-// The function is used to define various log levels and their properties
|
|
|
const logSeverity = (ordinal, label, esc, winst) => ({ ordinal, label, esc, winst });
|
|
|
-
|
|
|
-// TODO: support the following annotation in tools/comment-writer/main.js
|
|
|
-// AI-COMMENT-WRITER // SKIP 7 LINES
|
|
|
const LOG_LEVEL_ERRO = logSeverity(0, 'ERRO', '31;1', 'error');
|
|
|
const LOG_LEVEL_WARN = logSeverity(1, 'WARN', '33;1', 'warn');
|
|
|
const LOG_LEVEL_INFO = logSeverity(2, 'INFO', '36;1', 'info');
|
|
@@ -49,11 +44,10 @@ const WINSTON_LEVELS = {
|
|
|
|
|
|
|
|
|
/**
|
|
|
+* Represents a logging context within the LogService.
|
|
|
+* This class is used to manage logging operations with specific context information,
|
|
|
+* allowing for hierarchical logging structures and dynamic field additions.
|
|
|
* @class LogContext
|
|
|
-* @classdesc The LogContext class provides a structured way to handle logging within the application.
|
|
|
-* It encapsulates the logging service, breadcrumbs for context, and fields for additional information.
|
|
|
-* This class includes methods for different logging levels such as info, warn, debug, error, tick,
|
|
|
-* and system logs. It also provides utility methods for sub-contexts, caching, and trace identification.
|
|
|
*/
|
|
|
class LogContext {
|
|
|
constructor (logService, { crumbs, fields }) {
|
|
@@ -110,14 +104,11 @@ class LogContext {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- // convenience method to get a trace id that isn't as difficult
|
|
|
- // for a human to read as a uuid.
|
|
|
/**
|
|
|
- * Generates a human-readable trace ID.
|
|
|
- * This method creates a trace ID that is easier for humans to read compared to a UUID.
|
|
|
- * The trace ID is composed of two random alphanumeric strings joined by a hyphen.
|
|
|
- *
|
|
|
- * @returns {string} A human-readable trace ID.
|
|
|
+ * Generates a human-readable trace ID for logging purposes.
|
|
|
+ *
|
|
|
+ * @returns {string} A trace ID in the format 'xxxxxx-xxxxxx' where each segment is a
|
|
|
+ * random string of six lowercase letters and digits.
|
|
|
*/
|
|
|
mkid () {
|
|
|
// generate trace id
|
|
@@ -128,13 +119,9 @@ class LogContext {
|
|
|
return trace_id.join('-');
|
|
|
}
|
|
|
|
|
|
- // add a trace id to this logging context
|
|
|
/**
|
|
|
- * Adds a trace ID to the logging context.
|
|
|
- * This method generates a new trace ID and assigns it to the logging context's fields.
|
|
|
- * It then returns the modified logging context.
|
|
|
- *
|
|
|
- * @returns {LogContext} The modified logging context with the trace ID added.
|
|
|
+ * Adds a trace id to this logging context for tracking purposes.
|
|
|
+ * @returns {LogContext} The current logging context with the trace id added.
|
|
|
*/
|
|
|
traceOn () {
|
|
|
this.fields.trace_id = this.mkid();
|
|
@@ -143,10 +130,10 @@ class LogContext {
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * Retrieves the current log buffer.
|
|
|
- *
|
|
|
- * @returns {Array} The current log buffer containing log entries.
|
|
|
- */
|
|
|
+ * Gets the log buffer maintained by the LogService. This shows the most
|
|
|
+ * recent log entries.
|
|
|
+ * @returns {Array} An array of log entries stored in the buffer.
|
|
|
+ */
|
|
|
get_log_buffer () {
|
|
|
return this.logService.get_log_buffer();
|
|
|
}
|
|
@@ -154,29 +141,22 @@ class LogContext {
|
|
|
|
|
|
let log_epoch = Date.now();
|
|
|
/**
|
|
|
-* Function to initialize the log epoch timestamp.
|
|
|
-* This timestamp is used to calculate the time difference for log entries.
|
|
|
+* Timestamp in milliseconds since the epoch, used for calculating log entry duration.
|
|
|
*/
|
|
|
const stringify_log_entry = ({ prefix, log_lvl, crumbs, message, fields, objects }) => {
|
|
|
const { colorize } = require('json-colorizer');
|
|
|
|
|
|
let lines = [], m;
|
|
|
/**
|
|
|
- * Converts a log entry into a formatted string for display.
|
|
|
- *
|
|
|
- * This method formats log entries by combining the prefix, log level, crumbs (breadcrumbs),
|
|
|
- * message, fields, and objects into a readable string. It includes color coding for log levels
|
|
|
- * and timestamp information if available. The method processes each log entry into a multi-line
|
|
|
- * string for enhanced readability.
|
|
|
- *
|
|
|
- * @param {Object} entry - The log entry object to be stringified.
|
|
|
- * @param {string} entry.prefix - The optional prefix to prepend to the log message.
|
|
|
- * @param {Object} entry.log_lvl - The log level object containing label and escape sequences.
|
|
|
- * @param {Array} entry.crumbs - An array of breadcrumbs for context.
|
|
|
- * @param {string} entry.message - The main log message.
|
|
|
- * @param {Object} entry.fields - Additional fields to include in the log entry.
|
|
|
- * @param {Object} entry.objects - Additional objects to include in the log entry.
|
|
|
- * @returns {string} - The formatted log entry string.
|
|
|
+ * Stringifies a log entry into a formatted string for console output.
|
|
|
+ * @param {Object} logEntry - The log entry object containing:
|
|
|
+ * @param {string} [prefix] - Optional prefix for the log message.
|
|
|
+ * @param {Object} log_lvl - Log level object with properties for label, escape code, etc.
|
|
|
+ * @param {string[]} crumbs - Array of context crumbs.
|
|
|
+ * @param {string} message - The log message.
|
|
|
+ * @param {Object} fields - Additional fields to be included in the log.
|
|
|
+ * @param {Object} objects - Objects to be logged.
|
|
|
+ * @returns {string} A formatted string representation of the log entry.
|
|
|
*/
|
|
|
const lf = () => {
|
|
|
if ( ! m ) return;
|
|
@@ -214,10 +194,13 @@ const stringify_log_entry = ({ prefix, log_lvl, crumbs, message, fields, objects
|
|
|
|
|
|
/**
|
|
|
* @class DevLogger
|
|
|
-* @description The DevLogger class is responsible for handling logging operations in a development environment.
|
|
|
-* It can delegate logging to another logger and manage log output to a file. This class provides methods for
|
|
|
-* logging messages at different levels and managing the state of logging, such as turning logging on or off
|
|
|
-* and recording log output to a file. It is particularly useful for debugging and development purposes.
|
|
|
+* @classdesc
|
|
|
+* A development logger class designed for logging messages during development.
|
|
|
+* This logger can either log directly to console or delegate logging to another logger.
|
|
|
+* It provides functionality to turn logging on/off, and can optionally write logs to a file.
|
|
|
+*
|
|
|
+* @param {function} log - The logging function, typically `console.log` or similar.
|
|
|
+* @param {object} [opt_delegate] - An optional logger to which log messages can be delegated.
|
|
|
*/
|
|
|
class DevLogger {
|
|
|
// TODO: this should eventually delegate to winston logger
|
|
@@ -260,11 +243,10 @@ class DevLogger {
|
|
|
|
|
|
|
|
|
/**
|
|
|
-* @class
|
|
|
-* @classdesc The `NullLogger` class is a logging utility that does not perform any actual logging.
|
|
|
-* It is designed to be used as a placeholder or for environments where logging is not desired.
|
|
|
-* This class can be extended or used as a base for other logging implementations that need to
|
|
|
-* delegate logging responsibilities to another logger.
|
|
|
+* @class NullLogger
|
|
|
+* @description A logger that does nothing, effectively disabling logging.
|
|
|
+* This class is used when logging is not desired or during development
|
|
|
+* to avoid performance overhead or for testing purposes.
|
|
|
*/
|
|
|
class NullLogger {
|
|
|
// TODO: this should eventually delegate to winston logger
|
|
@@ -275,26 +257,15 @@ class NullLogger {
|
|
|
this.delegate = opt_delegate;
|
|
|
}
|
|
|
}
|
|
|
- /**
|
|
|
- * Constructor for the NullLogger class.
|
|
|
- * This method initializes a new instance of the NullLogger class.
|
|
|
- * It optionally accepts a delegate logger to which it can pass log messages.
|
|
|
- *
|
|
|
- * @param {function} log - The logging function to use (e.g., console.log).
|
|
|
- * @param {Object} opt_delegate - An optional delegate logger to pass log messages to.
|
|
|
- */
|
|
|
onLogMessage () {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
-* @class WinstonLogger
|
|
|
-* @classdesc The WinstonLogger class is responsible for integrating the Winston logging library
|
|
|
-* into the logging system. It handles forwarding log messages to Winston transports, which can
|
|
|
-* include various logging destinations such as files, consoles, and remote logging services.
|
|
|
-* This class is a key component in ensuring that log messages are appropriately recorded and
|
|
|
-* managed, providing a structured and configurable logging mechanism.
|
|
|
+* WinstonLogger Class
|
|
|
+*
|
|
|
+* A logger that delegates log messages to a Winston logger instance.
|
|
|
*/
|
|
|
class WinstonLogger {
|
|
|
constructor (winst) {
|
|
@@ -311,99 +282,13 @@ class WinstonLogger {
|
|
|
}
|
|
|
|
|
|
|
|
|
-/**
|
|
|
-* @class LogContext
|
|
|
-* @description The `LogContext` class provides a context for logging messages within the application.
|
|
|
-* It encapsulates the log service, a list of breadcrumbs (contextual information for the logs),
|
|
|
-* and fields that can be attached to log messages.
|
|
|
-*
|
|
|
-* This class includes methods for various log levels (info, warn, debug, error, etc.),
|
|
|
-* allowing for structured logging with contextual information.
|
|
|
-*
|
|
|
-* It also provides methods for creating sub-contexts, generating trace IDs, and managing log buffers.
|
|
|
-*/
|
|
|
-/**
|
|
|
-* @class DevLogger
|
|
|
-* @description The `DevLogger` class is a simple logger that outputs log messages to the console.
|
|
|
-* It is primarily used in development environments. This logger can also delegate log messages to another logger.
|
|
|
-*
|
|
|
-* The class includes methods for toggling log output, recording log messages to a file,
|
|
|
-* and adding timestamps to log entries.
|
|
|
-*/
|
|
|
-/**
|
|
|
-* @class NullLogger
|
|
|
-* @description The `NullLogger` class is a logger that does not output any log messages.
|
|
|
-* It is used when logging is disabled or when logging is not required.
|
|
|
-*/
|
|
|
-/**
|
|
|
-* @class WinstonLogger
|
|
|
-* @description The `WinstonLogger` class is a logger that integrates with the Winston logging library.
|
|
|
-* It provides a structured way to log messages with different log levels and transports.
|
|
|
-*
|
|
|
-* This logger can be used to log messages to files, with options for daily rotation and compression.
|
|
|
-*/
|
|
|
/**
|
|
|
* @class TimestampLogger
|
|
|
-* @description The `TimestampLogger` class is a decorator logger that adds timestamps to log messages.
|
|
|
-* It delegates the actual logging to another logger.
|
|
|
-*
|
|
|
-* This class ensures that each log message includes a timestamp, which can be useful for debugging and performance monitoring.
|
|
|
-*/
|
|
|
-/**
|
|
|
-* @class BufferLogger
|
|
|
-* @description The `BufferLogger` class is a logger that maintains a buffer of log messages.
|
|
|
-* It delegates the actual logging to another logger.
|
|
|
-*
|
|
|
-* This logger can be used to keep a limited number of recent log messages in memory,
|
|
|
-* which can be useful for debugging and troubleshooting.
|
|
|
-*/
|
|
|
-/**
|
|
|
-* @class CustomLogger
|
|
|
-* @description The `CustomLogger` class is a flexible logger that allows for custom log message processing.
|
|
|
-* It delegates the actual logging to another logger.
|
|
|
-*
|
|
|
-* This logger can be used to modify log messages before they are logged,
|
|
|
-* allowing for custom log message formatting and processing.
|
|
|
-*/
|
|
|
-/**
|
|
|
-* @class LogService
|
|
|
-* @description The `LogService` class is a service that provides logging functionality for the application.
|
|
|
-* It manages a collection of loggers, a buffer of recent log messages, and log directories.
|
|
|
-*
|
|
|
-* This class includes methods for registering log middleware, creating log contexts,
|
|
|
-* and logging messages at various log levels. It also ensures that log messages are only output if they are at or above the configured output level.
|
|
|
-*
|
|
|
-* The `LogService` class is responsible for initializing loggers based on the application's configuration,
|
|
|
-* ensuring that log directories exist, and providing methods for retrieving log files and buffers.
|
|
|
-*/
|
|
|
-/**
|
|
|
-* @class
|
|
|
-* @description The `TimestampLogger` class is a logger that adds timestamps to log messages. It delegates the actual logging to another logger.
|
|
|
-* This class ensures that each log message includes a timestamp, which can be useful for debugging and performance monitoring.
|
|
|
-*/
|
|
|
-/**
|
|
|
-* @class BufferLogger
|
|
|
-* @description The `BufferLogger` class is a logger that maintains a buffer of log messages. It delegates the actual logging to another logger.
|
|
|
-* This logger can be used to keep a limited number of recent log messages in memory, which can be useful for debugging and troubleshooting.
|
|
|
-*/
|
|
|
-/**
|
|
|
-* @class CustomLogger
|
|
|
-* @description The `CustomLogger` class is a flexible logger that allows for custom log message processing. It delegates the actual logging to another logger.
|
|
|
-* This logger can be used to modify log messages before they are logged, allowing for custom log message formatting and processing.
|
|
|
-*/
|
|
|
-/**
|
|
|
-* @class
|
|
|
-* @description The `LogService` class is a service that provides logging functionality for the application.
|
|
|
-* It manages a collection of loggers, a buffer of recent log messages, and log directories.
|
|
|
-* This class includes methods for registering log middleware, creating log contexts,
|
|
|
-* and logging messages at various log levels. It also ensures that log messages are only output if they are at or above the configured output level.
|
|
|
-* The `LogService` class is responsible for initializing loggers based on the application's configuration,
|
|
|
-* ensuring that log directories exist, and providing methods for retrieving log files and buffers.
|
|
|
-*/
|
|
|
-/**
|
|
|
-* @class
|
|
|
-* @description The `TimestampLogger` class is a logger that adds timestamps to log messages. It delegates the actual logging to another logger.
|
|
|
-* This class ensures that each log message includes a timestamp, which can be useful for debugging and performance monitoring.
|
|
|
+* @classdesc A logger that adds timestamps to log messages before delegating them to another logger.
|
|
|
+* This class wraps another logger instance to ensure that all log messages include a timestamp,
|
|
|
+* which can be useful for tracking the sequence of events in a system.
|
|
|
+*
|
|
|
+* @param {Object} delegate - The logger instance to which the timestamped log messages are forwarded.
|
|
|
*/
|
|
|
class TimestampLogger {
|
|
|
constructor (delegate) {
|
|
@@ -417,17 +302,12 @@ class TimestampLogger {
|
|
|
|
|
|
|
|
|
/**
|
|
|
-* The LogService class is a core service that manages logging across the application.
|
|
|
-* It facilitates the creation and management of various logging middleware, such as
|
|
|
-* DevLogger, NullLogger, WinstonLogger, and more. This class extends BaseService and
|
|
|
-* includes methods for initializing and configuring loggers, ensuring log directories,
|
|
|
-* and handling log messages. It also allows for the registration of custom log middleware
|
|
|
-* via the register_log_middleware method.
|
|
|
-*
|
|
|
-* The LogService class supports multiple logging levels, each with its own file and
|
|
|
-* transport mechanisms. It includes utility methods for creating new log contexts,
|
|
|
-* logging messages, and getting the log buffer. This class is essential for tracking
|
|
|
-* and monitoring application behavior, errors, and system events.
|
|
|
+* The `BufferLogger` class extends the logging functionality by maintaining a buffer of log entries.
|
|
|
+* This class is designed to:
|
|
|
+* - Store a specified number of recent log messages.
|
|
|
+* - Allow for retrieval of these logs for debugging or monitoring purposes.
|
|
|
+* - Ensure that the log buffer does not exceed the defined size by removing older entries when necessary.
|
|
|
+* - Delegate logging messages to another logger while managing its own buffer.
|
|
|
*/
|
|
|
class BufferLogger {
|
|
|
constructor (size, delegate) {
|
|
@@ -446,11 +326,11 @@ class BufferLogger {
|
|
|
|
|
|
|
|
|
/**
|
|
|
-* The `CustomLogger` class is a specialized logger that allows for custom
|
|
|
-* logging behavior by applying a callback function to modify log entries
|
|
|
-* before they are passed to the delegate logger. This class is part of the
|
|
|
-* logging infrastructure, providing flexibility to alter log messages, fields,
|
|
|
-* or other parameters dynamically based on the context in which the logging occurs.
|
|
|
+* Represents a custom logger that can modify log messages before they are passed to another logger.
|
|
|
+* @class CustomLogger
|
|
|
+* @extends {Object}
|
|
|
+* @param {Object} delegate - The delegate logger to which modified log messages will be passed.
|
|
|
+* @param {Function} callback - A callback function that modifies log parameters before delegation.
|
|
|
*/
|
|
|
class CustomLogger {
|
|
|
constructor (delegate, callback) {
|
|
@@ -486,21 +366,20 @@ class CustomLogger {
|
|
|
|
|
|
|
|
|
/**
|
|
|
-* The `LogService` class extends the `BaseService` and is responsible for managing logging operations.
|
|
|
-* It handles the registration of log middleware, initializes various logging mechanisms, and provides
|
|
|
-* methods to log messages at different severity levels. The class ensures that log directories are
|
|
|
-* properly set up and manages the logging output levels based on configuration.
|
|
|
+* The `LogService` class extends `BaseService` and is responsible for managing and
|
|
|
+* orchestrating various logging functionalities within the application. It handles
|
|
|
+* log initialization, middleware registration, log directory management, and
|
|
|
+* provides methods for creating log contexts and managing log output levels.
|
|
|
*/
|
|
|
class LogService extends BaseService {
|
|
|
static MODULES = {
|
|
|
path: require('path'),
|
|
|
}
|
|
|
/**
|
|
|
- * Initializes the log service by setting up the logging directory, configuring loggers,
|
|
|
- * and registering commands for log management.
|
|
|
- *
|
|
|
- * @async
|
|
|
- * @returns {Promise<void>} A promise that resolves when the initialization is complete.
|
|
|
+ * Defines the modules required by the LogService class.
|
|
|
+ * This static property contains modules that are used for file path operations.
|
|
|
+ * @property {Object} MODULES - An object containing required modules.
|
|
|
+ * @property {Object} MODULES.path - The Node.js path module for handling and resolving file paths.
|
|
|
*/
|
|
|
async _construct () {
|
|
|
this.loggers = [];
|
|
@@ -552,13 +431,13 @@ class LogService extends BaseService {
|
|
|
]);
|
|
|
}
|
|
|
/**
|
|
|
- * Registers log-related commands for the service.
|
|
|
- *
|
|
|
- * This method defines a set of commands for managing log output,
|
|
|
- * such as toggling log visibility, starting/stopping log recording to a file,
|
|
|
- * and toggling log indentation.
|
|
|
- *
|
|
|
- * @param {Object} commands - The commands object to register commands to.
|
|
|
+ * Registers logging commands with the command service.
|
|
|
+ *
|
|
|
+ * This method sets up various logging commands that can be used to
|
|
|
+ * interact with the log output, such as toggling log display,
|
|
|
+ * starting/stopping log recording, and toggling log indentation.
|
|
|
+ *
|
|
|
+ * @memberof LogService
|
|
|
*/
|
|
|
async _init () {
|
|
|
const config = this.global_config;
|
|
@@ -687,10 +566,12 @@ class LogService extends BaseService {
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * Ensures that the log directory exists by attempting to create it in several
|
|
|
- * predefined locations. If none of the locations are available, an error is thrown.
|
|
|
- *
|
|
|
- * @throws {Error} If the log directory cannot be created or found.
|
|
|
+ * Ensures that a log directory exists for logging purposes.
|
|
|
+ * This method attempts to create or locate a directory for log files,
|
|
|
+ * falling back through several predefined paths if the preferred
|
|
|
+ * directory does not exist or cannot be created.
|
|
|
+ *
|
|
|
+ * @throws {Error} If no suitable log directory can be found or created.
|
|
|
*/
|
|
|
ensure_log_directory_ () {
|
|
|
// STEP 1: Try /var/puter/logs/heyputer
|
|
@@ -749,12 +630,10 @@ class LogService extends BaseService {
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * Retrieves the log buffer.
|
|
|
- *
|
|
|
- * This method returns the current log buffer, which is an array of log entries.
|
|
|
- * Each log entry contains details such as the log level, crumbs, message, and fields.
|
|
|
- *
|
|
|
- * @returns {Array} The log buffer containing log entries.
|
|
|
+ * Generates a sanitized file path for log files.
|
|
|
+ *
|
|
|
+ * @param {string} name - The name of the log file, which will be sanitized to remove any path characters.
|
|
|
+ * @returns {string} A sanitized file path within the log directory.
|
|
|
*/
|
|
|
get_log_buffer () {
|
|
|
return this.bufferLogger.buffer;
|