All files / log / warn.ts

100.00% Branches 2/2
100.00% Lines 10/10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
 
 
 
x19
 
x19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x19
x19
x19
 
 
x27
x35
x35
x32
x27





































































// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.

import { getLogger } from "./get_logger.ts";
import type { GenericFunction } from "./logger.ts";
import "./setup.ts";

export type { GenericFunction };

/**
 * Log at the warning level.
 *
 * This function is a pass-through to the default logger's `warn` method. By
 * default, the default logger is configured to use {@linkcode console.log} and
 * print in yellow text.
 *
 * @template T The type of the message to log.
 * @param msg The message to log.
 * @param args Arguments to be formatted into the message.
 * @returns The message that was logged.
 *
 * @example Usage
 * ```ts
 * import { warn } from "@std/log/warn";
 * import { assertEquals } from "@std/assert/equals";
 *
 * assertEquals(warn("This is a warning message."), "This is a warning message.");
 * // Prints: "WARN This is a warning message."
 *
 * assertEquals(warn(() => "This is a warning message."), "This is a warning message.");
 * // Prints: "WARN This is a warning message."
 * ```
 */
export function warn<T>(msg: () => T, ...args: unknown[]): T | undefined;
/**
 * Log at the warning level.
 *
 * This function is a pass-through to the default logger's `warn` method. By
 * default, the default logger is configured to use {@linkcode console.log}.
 *
 * @template T The type of the message to log.
 * @param msg The message to log.
 * @param args Arguments to be formatted into the message.
 * @returns The message that was logged.
 *
 * @example Usage
 * ```ts
 * import { warn } from "@std/log/warn";
 * import { assertEquals } from "@std/assert/equals";
 *
 * assertEquals(warn("This is a warning message."), "This is a warning message.");
 * // Prints: "WARN This is a warning message."
 *
 * assertEquals(warn(() => "This is a warning message."), "This is a warning message.");
 * // Prints: "WARN This is a warning message."
 * ```
 */
export function warn<T>(
  msg: T extends GenericFunction ? never : T,
  ...args: unknown[]
): T;
export function warn<T>(
  msg: (T extends GenericFunction ? never : T) | (() => T),
  ...args: unknown[]
): T | undefined {
  // Assist TS compiler with pass-through generic type
  if (msg instanceof Function) {
    return getLogger("default").warn(msg, ...args);
  }
  return getLogger("default").warn(msg, ...args);
}