Переглянути джерело

ref: improve log functions

Sv443 1 рік тому
батько
коміт
c17c7a6464
2 змінених файлів з 11 додано та 9 видалено
  1. 2 1
      src/features/lyrics.ts
  2. 9 8
      src/utils.ts

+ 2 - 1
src/features/lyrics.ts

@@ -160,6 +160,7 @@ export async function getCurrentGeniusUrl() {
    */
 async function getGeniusUrl(artist: string, song: string): Promise<string | undefined> {
   try {
+    const startTs = Date.now();
     const fetchUrl = `${geniURLSearchTopUrl}?artist=${encodeURIComponent(artist)}&song=${encodeURIComponent(song)}`;
 
     log(`Requesting URL from geniURL at '${fetchUrl}'`);
@@ -173,7 +174,7 @@ async function getGeniusUrl(artist: string, song: string): Promise<string | unde
 
     const url = result.url;
 
-    info(`Found lyrics URL: ${url}`);
+    info(`Found lyrics URL (after ${Date.now() - startTs}ms): ${url}`);
 
     return url;
   }

+ 9 - 8
src/utils.ts

@@ -11,7 +11,7 @@ export function setLogLevel(level: LogLevel) {
   curLogLevel = level;
 }
 
-function getLogLevel(...args: unknown[]): number {
+function getLogLevel(args: unknown[]): number {
   if(typeof args.at(-1) === "number")
     return args.splice(args.length - 1, 1)[0] as number;
   return 0;
@@ -22,28 +22,28 @@ const consPrefix = `[${scriptInfo.name}]`;
 
 /**
  * Logs string-compatible values to the console, as long as the log level is sufficient.  
- * @param args Last parameter is logLevel: 0 = Debug, 1/undefined = Info
+ * @param args Last parameter is log level (0 = Debug, 1/undefined = Info)
  */
 export function log(...args: unknown[]): void {
-  if(curLogLevel <= getLogLevel(...args))
+  if(curLogLevel <= getLogLevel(args))
     console.log(consPrefix, ...args);
 }
 
 /**
  * Logs string-compatible values to the console as info, as long as the log level is sufficient.  
- * @param args Last parameter is logLevel: 0 = Debug, 1/undefined = Info
+ * @param args Last parameter is log level (0 = Debug, 1/undefined = Info)
  */
 export function info(...args: unknown[]): void {
-  if(curLogLevel <= getLogLevel(...args))
+  if(curLogLevel <= getLogLevel(args))
     console.info(consPrefix, ...args);
 }
 
 /**
  * Logs string-compatible values to the console as a warning, as long as the log level is sufficient.  
- * @param args Last parameter is logLevel: 0 = Debug, 1/undefined = Info
+ * @param args Last parameter is log level (0 = Debug, 1/undefined = Info)
  */
 export function warn(...args: unknown[]): void {
-  if(curLogLevel <= getLogLevel(...args))
+  if(curLogLevel <= getLogLevel(args))
     console.warn(consPrefix, ...args);
 }
 
@@ -186,7 +186,8 @@ export interface SiteEvents extends EventEmitter {
 export const siteEvents = new EventEmitter() as SiteEvents;
 
 /**
- * Returns the data of an event from the @billjs/event-emitter library
+ * Returns the data of an event from the `@billjs/event-emitter` library.  
+ * This function is used to assert the type passed in `<T>`
  * @param evt Event object from the `.on()` or `.once()` method
  * @template T Type of the data passed by `.fire(type: string, data: T)`
  */