2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-08-11 11:51:20 +00:00
|
|
|
|
2022-05-04 10:34:37 +00:00
|
|
|
/**
|
2022-08-11 11:51:20 +00:00
|
|
|
* Logging library with the support for terminal and file outputs. Also provides
|
2022-06-14 09:20:56 +00:00
|
|
|
* interfaces for building custom loggers.
|
2022-08-11 11:51:20 +00:00
|
|
|
*
|
2022-11-25 11:40:23 +00:00
|
|
|
* ## Loggers
|
|
|
|
*
|
|
|
|
* Loggers are objects that you interact with. When you use a logger method it
|
|
|
|
* constructs a `LogRecord` and passes it down to its handlers for output. To
|
|
|
|
* create custom loggers, specify them in `loggers` when calling `log.setup`.
|
|
|
|
*
|
|
|
|
* ## Custom message format
|
|
|
|
*
|
|
|
|
* If you want to override default format of message you can define `formatter`
|
2024-04-09 03:37:28 +00:00
|
|
|
* option for handler. It can a function that takes `LogRecord`
|
2022-11-25 11:40:23 +00:00
|
|
|
* as argument and outputs string.
|
|
|
|
*
|
|
|
|
* The default log format is `{levelName} {msg}`.
|
|
|
|
*
|
2023-12-18 11:19:47 +00:00
|
|
|
* ### Logging Structured JSON Lines
|
|
|
|
*
|
|
|
|
* To output logs in a structured JSON format you can configure most handlers
|
|
|
|
* with a formatter that produces a JSON string. Either use the premade
|
|
|
|
* `log.formatters.jsonFormatter` or write your own function that takes a
|
|
|
|
* {@linkcode LogRecord} and returns a JSON.stringify'd object.
|
|
|
|
* If you want the log to go to stdout then use {@linkcode ConsoleHandler} with
|
2024-09-16 22:48:56 +00:00
|
|
|
* the configuration `useColors: false` to turn off the ANSI terminal colors.
|
2023-12-18 11:19:47 +00:00
|
|
|
*
|
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import * as log from "@std/log";
|
2023-12-18 11:19:47 +00:00
|
|
|
*
|
|
|
|
* log.setup({
|
|
|
|
* handlers: {
|
2024-01-25 07:22:35 +00:00
|
|
|
* default: new log.ConsoleHandler("DEBUG", {
|
2023-12-18 11:19:47 +00:00
|
|
|
* formatter: log.formatters.jsonFormatter,
|
|
|
|
* useColors: false,
|
|
|
|
* }),
|
|
|
|
* },
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* The first argument passed to a log function is always treated as the
|
|
|
|
* message and will be stringified differently. To have arguments JSON.stringify'd
|
|
|
|
* you must pass them after the first.
|
|
|
|
*
|
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import * as log from "@std/log";
|
2023-12-18 11:19:47 +00:00
|
|
|
*
|
|
|
|
* log.info("This is the message", { thisWillBe: "JSON.stringify'd"});
|
|
|
|
* // {"level":"INFO","datetime":1702501580505,"message":"This is the message","args":{"thisWillBe":"JSON.stringify'd"}}
|
|
|
|
*
|
|
|
|
* log.info({ thisWontBe: "JSON.stringify'd"}, "This is an argument");
|
|
|
|
* // {"level":"INFO","datetime":1702501580505,"message":"{\"thisWontBe\":\"JSON.stringify'd\"}","args":"This is an argument"}
|
|
|
|
* ```
|
|
|
|
*
|
2022-11-25 11:40:23 +00:00
|
|
|
* ## Inline Logging
|
|
|
|
*
|
|
|
|
* Log functions return the data passed in the `msg` parameter. Data is returned
|
|
|
|
* regardless if the logger actually logs it.
|
|
|
|
*
|
|
|
|
* ## Lazy Log Evaluation
|
|
|
|
*
|
|
|
|
* Some log statements are expensive to compute. In these cases, you can use
|
|
|
|
* lazy log evaluation to prevent the computation taking place if the logger
|
|
|
|
* won't log the message.
|
|
|
|
*
|
|
|
|
* > NOTE: When using lazy log evaluation, `undefined` will be returned if the
|
|
|
|
* > resolver function is not called because the logger won't log it. It is an
|
|
|
|
* > antipattern use lazy evaluation with inline logging because the return value
|
|
|
|
* > depends on the current log level.
|
|
|
|
*
|
|
|
|
* ## For module authors
|
|
|
|
*
|
|
|
|
* The authors of public modules can let the users display the internal logs of the
|
|
|
|
* module by using a custom logger:
|
|
|
|
*
|
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { getLogger } from "@std/log";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* function logger() {
|
|
|
|
* return getLogger("my-awesome-module");
|
|
|
|
* }
|
|
|
|
*
|
2024-09-19 23:29:31 +00:00
|
|
|
* function sum(a: number, b: number) {
|
2022-11-25 11:40:23 +00:00
|
|
|
* logger().debug(`running ${a} + ${b}`);
|
|
|
|
* return a + b;
|
|
|
|
* }
|
|
|
|
*
|
2024-09-19 23:29:31 +00:00
|
|
|
* function mult(a: number, b: number) {
|
2022-11-25 11:40:23 +00:00
|
|
|
* logger().debug(`running ${a} * ${b}`);
|
|
|
|
* return a * b;
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* The user of the module can then display the internal logs with:
|
|
|
|
*
|
|
|
|
* ```ts, ignore
|
2024-04-29 02:57:30 +00:00
|
|
|
* import * as log from "@std/log";
|
2022-11-25 11:40:23 +00:00
|
|
|
* import { sum } from "<the-awesome-module>/mod.ts";
|
|
|
|
*
|
2023-08-29 07:48:52 +00:00
|
|
|
* log.setup({
|
2022-11-25 11:40:23 +00:00
|
|
|
* handlers: {
|
2024-01-25 07:22:35 +00:00
|
|
|
* console: new log.ConsoleHandler("DEBUG"),
|
2022-11-25 11:40:23 +00:00
|
|
|
* },
|
|
|
|
*
|
|
|
|
* loggers: {
|
|
|
|
* "my-awesome-module": {
|
|
|
|
* level: "DEBUG",
|
|
|
|
* handlers: ["console"],
|
|
|
|
* },
|
|
|
|
* },
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* sum(1, 2); // prints "running 1 + 2" to the console
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Please note that, due to the order of initialization of the loggers, the
|
|
|
|
* following won't work:
|
|
|
|
*
|
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { getLogger } from "@std/log";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* const logger = getLogger("my-awesome-module");
|
|
|
|
*
|
2024-09-19 23:29:31 +00:00
|
|
|
* function sum(a: number, b: number) {
|
2022-11-25 11:40:23 +00:00
|
|
|
* logger.debug(`running ${a} + ${b}`); // no message will be logged, because getLogger() was called before log.setup()
|
|
|
|
* return a + b;
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-04-29 02:57:30 +00:00
|
|
|
* import * as log from "@std/log";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* // Simple default logger out of the box. You can customize it
|
|
|
|
* // by overriding logger and handler named "default", or providing
|
|
|
|
* // additional logger configurations. You can log any data type.
|
|
|
|
* log.debug("Hello world");
|
|
|
|
* log.info(123456);
|
2024-01-11 01:36:15 +00:00
|
|
|
* log.warn(true);
|
2022-11-25 11:40:23 +00:00
|
|
|
* log.error({ foo: "bar", fizz: "bazz" });
|
|
|
|
* log.critical("500 Internal server error");
|
|
|
|
*
|
|
|
|
* // custom configuration with 2 loggers (the default and `tasks` loggers).
|
2023-08-29 07:48:52 +00:00
|
|
|
* log.setup({
|
2022-11-25 11:40:23 +00:00
|
|
|
* handlers: {
|
2024-01-25 07:22:35 +00:00
|
|
|
* console: new log.ConsoleHandler("DEBUG"),
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-02-02 05:57:14 +00:00
|
|
|
* file: new log.FileHandler("WARN", {
|
2022-11-25 11:40:23 +00:00
|
|
|
* filename: "./log.txt",
|
|
|
|
* // you can change format of output message using any keys in `LogRecord`.
|
2023-12-30 09:59:36 +00:00
|
|
|
* formatter: (record) => `${record.levelName} ${record.msg}`,
|
2022-11-25 11:40:23 +00:00
|
|
|
* }),
|
|
|
|
* },
|
|
|
|
*
|
|
|
|
* loggers: {
|
|
|
|
* // configure default logger available via short-hand methods above.
|
|
|
|
* default: {
|
|
|
|
* level: "DEBUG",
|
|
|
|
* handlers: ["console", "file"],
|
|
|
|
* },
|
|
|
|
*
|
|
|
|
* tasks: {
|
|
|
|
* level: "ERROR",
|
|
|
|
* handlers: ["console"],
|
|
|
|
* },
|
|
|
|
* },
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* let logger;
|
|
|
|
*
|
|
|
|
* // get default logger.
|
|
|
|
* logger = log.getLogger();
|
2024-02-02 05:57:14 +00:00
|
|
|
* logger.debug("fizz"); // logs to `console`, because `file` handler requires "WARN" level.
|
2024-01-11 01:36:15 +00:00
|
|
|
* logger.warn(41256); // logs to both `console` and `file` handlers.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* // get custom logger
|
|
|
|
* logger = log.getLogger("tasks");
|
|
|
|
* logger.debug("fizz"); // won't get output because this logger has "ERROR" level.
|
|
|
|
* logger.error({ productType: "book", value: "126.11" }); // log to `console`.
|
|
|
|
*
|
|
|
|
* // if you try to use a logger that hasn't been configured
|
|
|
|
* // you're good to go, it gets created automatically with level set to 0
|
|
|
|
* // so no message is logged.
|
|
|
|
* const unknownLogger = log.getLogger("mystery");
|
|
|
|
* unknownLogger.info("foobar"); // no-op
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* Custom message format example
|
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import * as log from "@std/log";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2023-08-29 07:48:52 +00:00
|
|
|
* log.setup({
|
2022-11-25 11:40:23 +00:00
|
|
|
* handlers: {
|
2024-01-25 07:22:35 +00:00
|
|
|
* stringFmt: new log.ConsoleHandler("DEBUG", {
|
2023-12-30 09:59:36 +00:00
|
|
|
* formatter: (record) => `[${record.levelName}] ${record.msg}`,
|
2022-11-25 11:40:23 +00:00
|
|
|
* }),
|
|
|
|
*
|
2024-01-25 07:22:35 +00:00
|
|
|
* functionFmt: new log.ConsoleHandler("DEBUG", {
|
2022-11-25 11:40:23 +00:00
|
|
|
* formatter: (logRecord) => {
|
|
|
|
* let msg = `${logRecord.level} ${logRecord.msg}`;
|
|
|
|
*
|
|
|
|
* logRecord.args.forEach((arg, index) => {
|
|
|
|
* msg += `, arg${index}: ${arg}`;
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* return msg;
|
|
|
|
* },
|
|
|
|
* }),
|
|
|
|
*
|
2024-01-25 07:22:35 +00:00
|
|
|
* anotherFmt: new log.ConsoleHandler("DEBUG", {
|
2023-12-30 09:59:36 +00:00
|
|
|
* formatter: (record) => `[${record.loggerName}] - ${record.levelName} ${record.msg}`,
|
2022-11-25 11:40:23 +00:00
|
|
|
* }),
|
|
|
|
* },
|
|
|
|
*
|
|
|
|
* loggers: {
|
|
|
|
* default: {
|
|
|
|
* level: "DEBUG",
|
|
|
|
* handlers: ["stringFmt", "functionFmt"],
|
|
|
|
* },
|
|
|
|
* dataLogger: {
|
|
|
|
* level: "INFO",
|
|
|
|
* handlers: ["anotherFmt"],
|
|
|
|
* },
|
|
|
|
* },
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* // calling:
|
|
|
|
* log.debug("Hello, world!", 1, "two", [3, 4, 5]);
|
|
|
|
* // results in: [DEBUG] Hello, world!
|
|
|
|
* // output from "stringFmt" handler.
|
|
|
|
* // 10 Hello, world!, arg0: 1, arg1: two, arg3: [3, 4, 5] // output from "functionFmt" formatter.
|
|
|
|
*
|
|
|
|
* // calling:
|
|
|
|
* log.getLogger("dataLogger").error("oh no!");
|
|
|
|
* // results in:
|
|
|
|
* // [dataLogger] - ERROR oh no! // output from anotherFmt handler.
|
|
|
|
* ```
|
2023-12-18 11:19:47 +00:00
|
|
|
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* JSON to stdout with no color example
|
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import * as log from "@std/log";
|
2023-12-18 11:19:47 +00:00
|
|
|
*
|
|
|
|
* log.setup({
|
|
|
|
* handlers: {
|
2024-01-25 07:22:35 +00:00
|
|
|
* jsonStdout: new log.ConsoleHandler("DEBUG", {
|
2023-12-18 11:19:47 +00:00
|
|
|
* formatter: log.formatters.jsonFormatter,
|
|
|
|
* useColors: false,
|
|
|
|
* }),
|
|
|
|
* },
|
|
|
|
*
|
|
|
|
* loggers: {
|
|
|
|
* default: {
|
|
|
|
* level: "DEBUG",
|
|
|
|
* handlers: ["jsonStdout"],
|
|
|
|
* },
|
|
|
|
* },
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* // calling:
|
|
|
|
* log.info("Hey");
|
|
|
|
* // results in:
|
|
|
|
* // {"level":"INFO","datetime":1702481922294,"message":"Hey"}
|
|
|
|
*
|
|
|
|
* // calling:
|
|
|
|
* log.info("Hey", { product: "nail" });
|
|
|
|
* // results in:
|
|
|
|
* // {"level":"INFO","datetime":1702484111115,"message":"Hey","args":{"product":"nail"}}
|
|
|
|
*
|
|
|
|
* // calling:
|
|
|
|
* log.info("Hey", 1, "two", [3, 4, 5]);
|
|
|
|
* // results in:
|
|
|
|
* // {"level":"INFO","datetime":1702481922294,"message":"Hey","args":[1,"two",[3,4,5]]}
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* Custom JSON example
|
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import * as log from "@std/log";
|
2023-12-18 11:19:47 +00:00
|
|
|
*
|
|
|
|
* log.setup({
|
|
|
|
* handlers: {
|
2024-01-25 07:22:35 +00:00
|
|
|
* customJsonFmt: new log.ConsoleHandler("DEBUG", {
|
2023-12-18 11:19:47 +00:00
|
|
|
* formatter: (record) => JSON.stringify({
|
|
|
|
* lvl: record.level,
|
|
|
|
* msg: record.msg,
|
|
|
|
* time: record.datetime.toISOString(),
|
|
|
|
* name: record.loggerName,
|
|
|
|
* }),
|
|
|
|
* useColors: false,
|
|
|
|
* }),
|
|
|
|
* },
|
|
|
|
*
|
|
|
|
* loggers: {
|
|
|
|
* default: {
|
|
|
|
* level: "DEBUG",
|
|
|
|
* handlers: ["customJsonFmt"],
|
|
|
|
* },
|
|
|
|
* },
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* // calling:
|
|
|
|
* log.info("complete");
|
|
|
|
* // results in:
|
|
|
|
* // {"lvl":20,"msg":"complete","time":"2023-12-13T16:38:27.328Z","name":"default"}
|
|
|
|
* ```
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* Inline Logging
|
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import * as logger from "@std/log";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* const stringData: string = logger.debug("hello world");
|
|
|
|
* const booleanData: boolean = logger.debug(true, 1, "abc");
|
|
|
|
* const fn = (): number => {
|
|
|
|
* return 123;
|
|
|
|
* };
|
|
|
|
* const resolvedFunctionData: number = logger.debug(fn());
|
|
|
|
* console.log(stringData); // 'hello world'
|
|
|
|
* console.log(booleanData); // true
|
|
|
|
* console.log(resolvedFunctionData); // 123
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* Lazy Log Evaluation
|
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import * as log from "@std/log";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2023-08-29 07:48:52 +00:00
|
|
|
* log.setup({
|
2022-11-25 11:40:23 +00:00
|
|
|
* handlers: {
|
2024-01-25 07:22:35 +00:00
|
|
|
* console: new log.ConsoleHandler("DEBUG"),
|
2022-11-25 11:40:23 +00:00
|
|
|
* },
|
|
|
|
*
|
|
|
|
* loggers: {
|
|
|
|
* tasks: {
|
|
|
|
* level: "ERROR",
|
|
|
|
* handlers: ["console"],
|
|
|
|
* },
|
|
|
|
* },
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* function someExpensiveFn(num: number, bool: boolean) {
|
|
|
|
* // do some expensive computation
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // not logged, as debug < error.
|
|
|
|
* const data = log.debug(() => someExpensiveFn(5, true));
|
|
|
|
* console.log(data); // undefined
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Handlers are responsible for actual output of log messages. When a handler is
|
|
|
|
* called by a logger, it firstly checks that {@linkcode LogRecord}'s level is
|
|
|
|
* not lower than level of the handler. If level check passes, handlers formats
|
|
|
|
* log record into string and outputs it to target.
|
|
|
|
*
|
|
|
|
* ## Custom handlers
|
|
|
|
*
|
|
|
|
* Custom handlers can be implemented by subclassing {@linkcode BaseHandler} or
|
|
|
|
* {@linkcode WriterHandler}.
|
|
|
|
*
|
|
|
|
* {@linkcode BaseHandler} is bare-bones handler that has no output logic at all,
|
|
|
|
*
|
|
|
|
* {@linkcode WriterHandler} is an abstract class that supports any target with
|
|
|
|
* `Writer` interface.
|
|
|
|
*
|
|
|
|
* During setup async hooks `setup` and `destroy` are called, you can use them
|
|
|
|
* to open and close file/HTTP connection or any other action you might need.
|
|
|
|
*
|
|
|
|
* For examples check source code of {@linkcode FileHandler}`
|
|
|
|
* and {@linkcode TestHandler}.
|
2024-01-11 01:36:15 +00:00
|
|
|
*
|
2024-02-02 00:35:43 +00:00
|
|
|
* @module
|
2024-01-11 01:36:15 +00:00
|
|
|
*/
|
2019-01-02 14:12:48 +00:00
|
|
|
|
2024-02-02 00:35:43 +00:00
|
|
|
export * from "./base_handler.ts";
|
|
|
|
export * from "./console_handler.ts";
|
|
|
|
export * from "./file_handler.ts";
|
|
|
|
export * from "./rotating_file_handler.ts";
|
|
|
|
export * from "./levels.ts";
|
|
|
|
export * from "./logger.ts";
|
|
|
|
export * from "./formatters.ts";
|
|
|
|
export * from "./critical.ts";
|
|
|
|
export * from "./debug.ts";
|
|
|
|
export * from "./error.ts";
|
|
|
|
export * from "./get_logger.ts";
|
|
|
|
export * from "./info.ts";
|
|
|
|
export * from "./setup.ts";
|
|
|
|
export * from "./warn.ts";
|