Skip to content

Commit

Permalink
customFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
Shirtiny committed Jan 29, 2023
1 parent 0dc1cf1 commit f177eb2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) => {
Expand All @@ -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();
Expand Down
20 changes: 18 additions & 2 deletions src/model/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
}
Expand Down

0 comments on commit f177eb2

Please sign in to comment.