var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _ConsoleHandler_useColors;
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
import { LogLevels } from "./levels.js";
import { blue, bold, red, yellow } from "../../fmt/1.0.8/colors.js";
import { BaseHandler } from "./base_handler.js";
function applyColors(msg, level) {
    switch (level) {
        case LogLevels.INFO:
            msg = blue(msg);
            break;
        case LogLevels.WARN:
            msg = yellow(msg);
            break;
        case LogLevels.ERROR:
            msg = red(msg);
            break;
        case LogLevels.CRITICAL:
            msg = bold(red(msg));
            break;
        default:
            break;
    }
    return msg;
}
/**
 * Default logger that outputs log messages to the console via
 * {@linkcode console.log}.
 *
 * @example Usage
 * ```ts no-assert
 * import { ConsoleHandler } from "@std/log/console-handler";
 *
 * const handler = new ConsoleHandler("INFO");
 * handler.log("Hello, world!"); // Prints "Hello, world!"
 * ```
 */
export class ConsoleHandler extends BaseHandler {
    /**
     * Constructs a new instance.
     *
     * @param levelName The level name to log messages at.
     * @param options Options for the handler.
     */
    constructor(levelName, options = {}) {
        super(levelName, options);
        _ConsoleHandler_useColors.set(this, void 0);
        __classPrivateFieldSet(this, _ConsoleHandler_useColors, options.useColors ?? true, "f");
    }
    /**
     * Formats a log record into a string.
     *
     * @example Usage
     * ```ts
     * import { ConsoleHandler } from "@std/log/console-handler";
     * import { LogRecord } from "@std/log/logger";
     * import { LogLevels } from "@std/log/levels";
     * import { assertEquals } from "@std/assert/equals";
     * import { blue } from "@std/fmt/colors";
     *
     * const handler = new ConsoleHandler("INFO");
     * const logRecord = new LogRecord({
     *   msg: "Hello, world!",
     *   args: ["foo", "bar"],
     *   level: LogLevels.INFO,
     *   loggerName: "my-logger",
     * });
     * const result = handler.format(logRecord);
     *
     * assertEquals(result, blue("INFO Hello, world!"));
     * ```
     *
     * @param logRecord The log record to format.
     * @returns The formatted log record.
     */
    format(logRecord) {
        let msg = super.format(logRecord);
        if (__classPrivateFieldGet(this, _ConsoleHandler_useColors, "f")) {
            msg = applyColors(msg, logRecord.level);
        }
        return msg;
    }
    /**
     * Logs a message to the console.
     *
     * @example Usage
     * ```ts no-assert
     * import { ConsoleHandler } from "@std/log/console-handler";
     *
     * const handler = new ConsoleHandler("INFO");
     * handler.log("Hello, world!"); // Prints "Hello, world!"
     * ```
     *
     * @param msg The message to log.
     */
    log(msg) {
        // deno-lint-ignore no-console
        console.log(msg);
    }
}
_ConsoleHandler_useColors = new WeakMap();
