@fiberplane/hono-otel
Version:
Hono middleware to forward OpenTelemetry traces to a local instance of @fiberplane/studio
66 lines (65 loc) • 2.69 kB
JavaScript
// Define the possible log levels
const logLevels = ["debug", "info", "warn", "error"];
const debugLog = console.debug.bind(console);
const infoLog = console.info.bind(console);
const warnLog = console.warn.bind(console);
const errorLog = console.error.bind(console);
const ourConsole = {
debug: debugLog,
info: infoLog,
warn: warnLog,
error: errorLog,
};
/**
* Get a logger that can be used to log messages to the console.
* - Optionally log depending on the FPX_LOG_LEVEL environment variable.
* - By default, log everything when in development mode, and log "warn" and above otherwise.
*
* @OPTIMIZE - This creates a new logger every time it's called.
* (But this is somewhat necessary, because the logger needs to be able to access the environment variables.
* In CF Workers, the environment variables are only available at runtime,
* and a single worker's env might get reused across requests.)
*
* @param honoEnv - The environment variables from the Hono app.
* @returns A logger object with methods for each log level
*/
export function getLogger(level) {
// Determine the current log level from the environment variable or default to "warn"
const defaultLogLevel = "warn";
const currentLogLevel = isLogLevel(level) ? level : defaultLogLevel;
/**
* Determines if a message at a given log level should be logged based on the current log level.
* @param level - The log level of the message.
* @returns True if the message should be logged, false otherwise.
*/
function shouldLog(level) {
const currentLevelIndex = logLevels.indexOf(currentLogLevel);
const messageLevelIndex = logLevels.indexOf(level);
return messageLevelIndex >= currentLevelIndex;
}
// Create a logger object with methods for each log level
const logger = logLevels.reduce((acc, level) => {
/**
* Logs a message if the current log level allows it.
* Prefix the log message with "[@fiberplane]" to make it easy to grep.
*
* @param message - The message to log.
* @param optionalParams - Additional parameters to log.
*/
acc[level] = (message, ...optionalParams) => {
if (shouldLog(level)) {
ourConsole[level]("[@fiberplane/hono-otel]", message, ...optionalParams);
}
};
return acc;
}, {});
return logger;
}
/**
* Checks if a given level is a valid log level.
* @param level - The level to check.
* @returns True if the level is a valid LogLevel, false otherwise.
*/
function isLogLevel(level) {
return logLevels.includes(level);
}