From f177eb2b2beb041e817b4bdf4080dcc29e1df83e Mon Sep 17 00:00:00 2001 From: Shirtiny <49592759+Shirtiny@users.noreply.github.com> Date: Sun, 29 Jan 2023 05:28:14 +0000 Subject: [PATCH] customFormat --- README.md | 55 ++++++++++++++++++++++++++++++++++++++++++++- src/model/logger.ts | 20 +++++++++++++++-- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0341ef6..866901c 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ enum LEVELS { - extends ShLogger for custom ```typescript -import { ShLogger } from "@shirtiny/logger"; +import { ShLogger, css } from "@shirtiny/logger"; class CustomerLogger extends ShLogger { custom = (message: string, ...data: any[]) => { @@ -106,6 +106,59 @@ class CustomerLogger extends ShLogger { ...data, ); }; + + custom2 = (message: string, ...data: any[]) => { + const level = 4; + this.formatLog( + level, + " Custom ", + message, + // style for " Custom ", + css` + color: #fff; + padding: 2px; + background-color: #3f6600; + border-radius: 3px; + margin-right: 8px; + `, + // style for message, + css` + color: #3f6600; + font-size: 15px; + font-family: "Trebuchet MS"; + `, + ...data, + ); + }; + + custom3 = (message: string, ...data: any[]) => { + const level = 4; + this.customFormat( + level, + [ + { + str: " Custom ", + style: css` + color: #fff; + padding: 2px; + background-color: #3f6600; + border-radius: 3px; + margin-right: 8px; + `, + }, + { + str: message, + style: css` + color: #3f6600; + font-size: 15px; + font-family: "Trebuchet MS"; + `, + } + ] + ...data, + ); + }; + } const customLogger = new CustomerLogger(); diff --git a/src/model/logger.ts b/src/model/logger.ts index 1045822..f778bc9 100644 --- a/src/model/logger.ts +++ b/src/model/logger.ts @@ -67,6 +67,23 @@ export class Logger extends BaseLogger { this.loggerOption.level = level; } + isLevelAllowed( level: number,) { + if (!this.loggerOption.level && this.loggerOption.level !== 0) return false; + if (this.loggerOption.level < level) return false; + return true; + } + + protected customFormat( + level: number, + pairs: {str: string, style: string}[], + ...data: any[] + ) { + if (!this.isLevelAllowed(level)) return; + const content = "%c" + pairs.map((p) => p.str).join("%c"); + const descStyles = pairs.map((p) => p.style) + super.log(content, ...descStyles, ...data); + } + protected formatLog( level: number, title: string, @@ -75,8 +92,7 @@ export class Logger extends BaseLogger { messageCss: string, ...data: any[] ) { - if (!this.loggerOption.level && this.loggerOption.level !== 0) return; - if (this.loggerOption.level < level) return; + if (!this.isLevelAllowed(level)) return; super.log(`%c${title}%c${message}`, titleCss, messageCss, ...data); } }