Skip to content

Commit

Permalink
docs(log): document getLogger() and Logger (#6084)
Browse files Browse the repository at this point in the history
  • Loading branch information
iuioiua authored Oct 3, 2024
1 parent b324bfa commit 065296c
Show file tree
Hide file tree
Showing 3 changed files with 377 additions and 3 deletions.
1 change: 1 addition & 0 deletions _tools/check_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const ENTRY_POINTS = [
"../log/info.ts",
"../log/console_handler.ts",
"../log/formatters.ts",
"../log/get_logger.ts",
"../media_types/mod.ts",
"../msgpack/mod.ts",
"../net/mod.ts",
Expand Down
58 changes: 57 additions & 1 deletion log/get_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,63 @@
import { Logger } from "./logger.ts";
import { state } from "./_state.ts";

/** Get a logger instance. If not specified `name`, get the default logger. */
export type { Logger };

/**
* Get a logger instance. If not specified `name`, get the default logger.
*
* @param name The name of the logger.
* @returns The logger instance.
*
* @example Usage (without defined name and minimal setup)
* ```ts
* import { getLogger } from "@std/log/get-logger";
* import "@std/log/setup";
* import { assertEquals } from "@std/assert/equals";
*
* const logger = getLogger();
* const result = logger.info("Hello world!"); // Prints "INFO Hello world!" in blue
*
* assertEquals(result, "Hello world!");
* ```
*
* @example Usage (without defined name and custom setup)
* ```ts
* import { getLogger } from "@std/log/get-logger";
* import { ConsoleHandler } from "@std/log/console-handler";
* import { setup } from "@std/log/setup";
* import { assertEquals } from "@std/assert/equals";
*
* setup({
* handlers: {
* console: new ConsoleHandler("DEBUG"),
* },
* loggers: {
* default: {
* level: "DEBUG",
* handlers: ["console"],
* },
* },
* });
*
* const logger = getLogger();
*
* const result = logger.info("Hello world!"); // Prints "INFO Hello world!" in blue
*
* assertEquals(result, "Hello world!");
* ```
*
* @example Usage (with defined name)
* ```ts
* import { getLogger } from "@std/log/get-logger";
* import { assertEquals } from "@std/assert/equals";
*
* const logger = getLogger("my-logger");
* const result = logger.info("Hello world!");
*
* assertEquals(result, "Hello world!");
* ```
*/
export function getLogger(name?: string): Logger {
if (!name) {
const d = state.loggers.get("default");
Expand Down
Loading

0 comments on commit 065296c

Please sign in to comment.