Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(log): document getLogger() and Logger #6084

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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