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

deprecation(log): rename warning() to warn() #4117

Merged
merged 19 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion log/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class ConsoleHandler extends BaseHandler {
case LogLevels.INFO:
msg = blue(msg);
break;
case LogLevels.WARNING:
case LogLevels.WARN:
msg = yellow(msg);
break;
case LogLevels.ERROR:
Expand Down
40 changes: 20 additions & 20 deletions log/handlers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Deno.test("simpleHandler", function () {
[
"DEBUG debug-test",
"INFO info-test",
"WARNING warning-test",
"WARN warn-test",
"ERROR error-test",
"CRITICAL critical-test",
],
Expand All @@ -42,14 +42,14 @@ Deno.test("simpleHandler", function () {
LogLevels.INFO,
[
"INFO info-test",
"WARNING warning-test",
"WARN warn-test",
"ERROR error-test",
"CRITICAL critical-test",
],
],
[
LogLevels.WARNING,
["WARNING warning-test", "ERROR error-test", "CRITICAL critical-test"],
LogLevels.WARN,
["WARN warn-test", "ERROR error-test", "CRITICAL critical-test"],
],
[LogLevels.ERROR, ["ERROR error-test", "CRITICAL critical-test"]],
[LogLevels.CRITICAL, ["CRITICAL critical-test"]],
Expand Down Expand Up @@ -164,7 +164,7 @@ Deno.test({
}
}

const testFileHandler = new TestFileHandler("WARNING", {
const testFileHandler = new TestFileHandler("WARN", {
filename: LOG_FILE,
mode: "w",
});
Expand All @@ -175,7 +175,7 @@ Deno.test({
new LogRecord({
msg: "The starry heavens above me and the moral law within me.",
args: [],
level: LogLevels.WARNING,
level: LogLevels.WARN,
loggerName: "default",
}),
);
Expand All @@ -188,7 +188,7 @@ Deno.test({
Deno.test({
name: "FileHandler with mode 'w' will wipe clean existing log file",
async fn() {
const fileHandler = new FileHandler("WARNING", {
const fileHandler = new FileHandler("WARN", {
filename: LOG_FILE,
mode: "w",
});
Expand All @@ -198,7 +198,7 @@ Deno.test({
new LogRecord({
msg: "Hello World",
args: [],
level: LogLevels.WARNING,
level: LogLevels.WARN,
loggerName: "default",
}),
);
Expand All @@ -210,7 +210,7 @@ Deno.test({
new LogRecord({
msg: "Hello World",
args: [],
level: LogLevels.WARNING,
level: LogLevels.WARN,
loggerName: "default",
}),
);
Expand All @@ -225,7 +225,7 @@ Deno.test({
Deno.test({
name: "FileHandler with mode 'x' will throw if log file already exists",
fn() {
const fileHandler = new FileHandler("WARNING", {
const fileHandler = new FileHandler("WARN", {
filename: LOG_FILE,
mode: "x",
});
Expand Down Expand Up @@ -259,7 +259,7 @@ Deno.test({
new TextEncoder().encode("hello world"),
);

const fileHandler = new RotatingFileHandler("WARNING", {
const fileHandler = new RotatingFileHandler("WARN", {
filename: LOG_FILE,
maxBytes: 50,
maxBackupCount: 3,
Expand All @@ -285,7 +285,7 @@ Deno.test({
LOG_FILE + ".3",
new TextEncoder().encode("hello world"),
);
const fileHandler = new RotatingFileHandler("WARNING", {
const fileHandler = new RotatingFileHandler("WARN", {
filename: LOG_FILE,
maxBytes: 50,
maxBackupCount: 3,
Expand All @@ -308,7 +308,7 @@ Deno.test({
Deno.test({
name: "RotatingFileHandler with first rollover, monitor step by step",
async fn() {
const fileHandler = new RotatingFileHandler("WARNING", {
const fileHandler = new RotatingFileHandler("WARN", {
filename: LOG_FILE,
maxBytes: 25,
maxBackupCount: 3,
Expand Down Expand Up @@ -358,7 +358,7 @@ Deno.test({
Deno.test({
name: "RotatingFileHandler with first rollover, check all at once",
async fn() {
const fileHandler = new RotatingFileHandler("WARNING", {
const fileHandler = new RotatingFileHandler("WARN", {
filename: LOG_FILE,
maxBytes: 25,
maxBackupCount: 3,
Expand Down Expand Up @@ -418,7 +418,7 @@ Deno.test({
new TextEncoder().encode("original log.3 file"),
);

const fileHandler = new RotatingFileHandler("WARNING", {
const fileHandler = new RotatingFileHandler("WARN", {
filename: LOG_FILE,
maxBytes: 2,
maxBackupCount: 3,
Expand Down Expand Up @@ -463,7 +463,7 @@ Deno.test({
fn() {
assertThrows(
() => {
const fileHandler = new RotatingFileHandler("WARNING", {
const fileHandler = new RotatingFileHandler("WARN", {
filename: LOG_FILE,
maxBytes: 0,
maxBackupCount: 3,
Expand All @@ -482,7 +482,7 @@ Deno.test({
fn() {
assertThrows(
() => {
const fileHandler = new RotatingFileHandler("WARNING", {
const fileHandler = new RotatingFileHandler("WARN", {
filename: LOG_FILE,
maxBytes: 50,
maxBackupCount: 0,
Expand All @@ -499,7 +499,7 @@ Deno.test({
Deno.test({
name: "Window unload flushes buffer",
async fn() {
const fileHandler = new FileHandler("WARNING", {
const fileHandler = new FileHandler("WARN", {
filename: LOG_FILE,
mode: "w",
});
Expand All @@ -525,7 +525,7 @@ Deno.test({
Deno.test({
name: "RotatingFileHandler: rotate on byte length, not msg length",
async fn() {
const fileHandler = new RotatingFileHandler("WARNING", {
const fileHandler = new RotatingFileHandler("WARN", {
filename: LOG_FILE,
maxBytes: 7,
maxBackupCount: 1,
Expand Down Expand Up @@ -559,7 +559,7 @@ Deno.test({
Deno.test({
name: "FileHandler: Critical logs trigger immediate flush",
async fn() {
const fileHandler = new FileHandler("WARNING", {
const fileHandler = new FileHandler("WARN", {
filename: LOG_FILE,
mode: "w",
});
Expand Down
4 changes: 2 additions & 2 deletions log/levels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const LogLevels = {
NOTSET: 0,
DEBUG: 10,
INFO: 20,
WARNING: 30,
WARN: 30,
ERROR: 40,
CRITICAL: 50,
} as const;
Expand All @@ -29,7 +29,7 @@ const byLevel: Record<LogLevel, LevelName> = {
[LogLevels.NOTSET]: "NOTSET",
[LogLevels.DEBUG]: "DEBUG",
[LogLevels.INFO]: "INFO",
[LogLevels.WARNING]: "WARNING",
[LogLevels.WARN]: "WARN",
[LogLevels.ERROR]: "ERROR",
[LogLevels.CRITICAL]: "CRITICAL",
};
Expand Down
13 changes: 12 additions & 1 deletion log/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,24 @@
return this.#_log(LogLevels.INFO, msg, ...args);
}

/** @deprecated (will be removed after 0.214.0) Use {@linkcode warn} instead. */
warning<T>(msg: () => T, ...args: unknown[]): T | undefined;
/** @deprecated (will be removed after 0.214.0) Use {@linkcode warn} instead. */
warning<T>(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T;
timreichen marked this conversation as resolved.
Show resolved Hide resolved
warning<T>(
msg: (T extends GenericFunction ? never : T) | (() => T),
...args: unknown[]
): T | undefined {
return this.#_log(LogLevels.WARNING, msg, ...args);
return this.warn(msg as () => T, ...args);
}

Check warning on line 179 in log/logger.ts

View check run for this annotation

Codecov / codecov/patch

log/logger.ts#L178-L179

Added lines #L178 - L179 were not covered by tests

warn<T>(msg: () => T, ...args: unknown[]): T | undefined;
warn<T>(msg: T extends GenericFunction ? never : T, ...args: unknown[]): T;
warn<T>(
msg: (T extends GenericFunction ? never : T) | (() => T),
...args: unknown[]
): T | undefined {
return this.#_log(LogLevels.WARN, msg, ...args);
}

error<T>(msg: () => T, ...args: unknown[]): T | undefined;
Expand Down
28 changes: 14 additions & 14 deletions log/logger_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ Deno.test("logFunctions", function () {
const logger = new Logger("default", level, { handlers: [handler] });
const debugData = logger.debug("foo");
const infoData = logger.info("bar");
const warningData = logger.warning("baz");
const warnData = logger.warn("baz");
const errorData = logger.error("boo");
const criticalData = logger.critical("doo");
assertEquals(debugData, "foo");
assertEquals(infoData, "bar");
assertEquals(warningData, "baz");
assertEquals(warnData, "baz");
assertEquals(errorData, "boo");
assertEquals(criticalData, "doo");
return handler;
Expand All @@ -87,7 +87,7 @@ Deno.test("logFunctions", function () {
assertEquals(handler.messages, [
"DEBUG foo",
"INFO bar",
"WARNING baz",
"WARN baz",
"ERROR boo",
"CRITICAL doo",
]);
Expand All @@ -96,14 +96,14 @@ Deno.test("logFunctions", function () {

assertEquals(handler.messages, [
"INFO bar",
"WARNING baz",
"WARN baz",
"ERROR boo",
"CRITICAL doo",
]);

handler = doLog("WARNING");
handler = doLog("WARN");

assertEquals(handler.messages, ["WARNING baz", "ERROR boo", "CRITICAL doo"]);
assertEquals(handler.messages, ["WARN baz", "ERROR boo", "CRITICAL doo"]);

handler = doLog("ERROR");

Expand Down Expand Up @@ -177,12 +177,12 @@ Deno.test(
assertEquals(handler.messages[3], "INFO null");

// number
const data5: number = logger.warning(3);
const data5: number = logger.warn(3);
assertEquals(data5, 3);
const data6: number = logger.warning(3, 1);
const data6: number = logger.warn(3, 1);
assertEquals(data6, 3);
assertEquals(handler.messages[4], "WARNING 3");
assertEquals(handler.messages[5], "WARNING 3");
assertEquals(handler.messages[4], "WARN 3");
assertEquals(handler.messages[5], "WARN 3");

// bigint
const data7: bigint = logger.error(5n);
Expand Down Expand Up @@ -217,12 +217,12 @@ Deno.test(
assertEquals(handler.messages[13], "INFO Symbol(a)");

// function
const data15: string | undefined = logger.warning(fn);
const data15: string | undefined = logger.warn(fn);
assertEquals(data15, "abc");
const data16: string | undefined = logger.warning(fn, 1);
const data16: string | undefined = logger.warn(fn, 1);
assertEquals(data16, "abc");
assertEquals(handler.messages[14], "WARNING abc");
assertEquals(handler.messages[15], "WARNING abc");
assertEquals(handler.messages[14], "WARN abc");
assertEquals(handler.messages[15], "WARN abc");

// object
const data17: { payload: string; other: number } = logger.error({
Expand Down
40 changes: 33 additions & 7 deletions log/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
* // additional logger configurations. You can log any data type.
* log.debug("Hello world");
* log.info(123456);
* log.warning(true);
* log.warn(true);
* log.error({ foo: "bar", fizz: "bazz" });
* log.critical("500 Internal server error");
*
Expand All @@ -148,7 +148,7 @@
* handlers: {
* console: new log.handlers.ConsoleHandler("DEBUG"),
*
* file: new log.handlers.FileHandler("WARNING", {
* file: new log.handlers.FileHandler("WARN", {
* filename: "./log.txt",
* // you can change format of output message using any keys in `LogRecord`.
* formatter: (record) => `${record.levelName} ${record.msg}`,
Expand All @@ -173,8 +173,8 @@
*
* // get default logger.
* logger = log.getLogger();
* logger.debug("fizz"); // logs to `console`, because `file` handler requires "WARNING" level.
* logger.warning(41256); // logs to both `console` and `file` handlers.
* logger.debug("fizz"); // logs to `console`, because `file` handler requires "WARN" level.
* logger.warn(41256); // logs to both `console` and `file` handlers.
*
* // get custom logger
* logger = log.getLogger("tasks");
Expand Down Expand Up @@ -498,8 +498,17 @@
return getLogger("default").info(msg, ...args);
}

/** Log with warning level, using default logger. */
/**
* @deprecated (will be removed after 0.214.0) Use {@linkcode warn} instead.
*
* Log with warning level, using default logger.
*/
export function warning<T>(msg: () => T, ...args: unknown[]): T | undefined;
/**
* @deprecated (will be removed after 0.214.0) Use {@linkcode warn} instead.
*
* Log with warning level, using default logger.
*/
export function warning<T>(
timreichen marked this conversation as resolved.
Show resolved Hide resolved
msg: T extends GenericFunction ? never : T,
...args: unknown[]
Expand All @@ -510,9 +519,26 @@
): T | undefined {
// Assist TS compiler with pass-through generic type
if (msg instanceof Function) {
return getLogger("default").warning(msg, ...args);
return warn(msg, ...args);
}
return warn(msg, ...args);
}

Check warning on line 525 in log/mod.ts

View check run for this annotation

Codecov / codecov/patch

log/mod.ts#L522-L525

Added lines #L522 - L525 were not covered by tests

/** Log with warning level, using default logger. */
export function warn<T>(msg: () => T, ...args: unknown[]): T | undefined;
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").warning(msg, ...args);
return getLogger("default").warn(msg, ...args);
}

/** Log with error level, using default logger. */
Expand Down
Loading