Skip to content

Commit

Permalink
feat(method)!: remove success and rename log type to log level
Browse files Browse the repository at this point in the history
  • Loading branch information
GloryWong committed Jun 27, 2024
1 parent 35e1b8c commit 939708d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ pnpm add @gloxy/logger

### Enabling and Disabling

- To enable the logger: `*` represents all log types (`info`, `success`, `warn`, `error`, and `debug`).
The only parameter `name` (e.g., 'myapp'), representing namespace, helps distinguish these logs from other prints.

- To enable the logger: `*` represents all log levels (`debug`, `info`, `warn`, and `error`).

* In browser: `localStorage.logger = 'myapp:*'`
* In Node.js: set the environment variable `LOGGER = myapp:* node index.js`

Specify a type to enable the single type of logger, e.g, `myapp:info`.
Specify a level name to enable a single level of logger, e.g, `myapp:info`.

- Disable logger by removing it.

Expand All @@ -53,7 +55,6 @@ disable()

```javascript
// Instantiate the logger at the beginning of your application.
// The only parameter `name` (e.g., 'myapp') helps distinguish these logs from other prints.
import { createLogger } from '@gloxy/logger'
const logger = createLogger('myapp')

Expand Down
48 changes: 21 additions & 27 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { isNode } from 'detect-node-es'
import debug from 'debug'
import type { Simplify } from 'type-fest'

type LoggerType = 'error' | 'warn' | 'success' | 'info' | 'debug'
type LoggerLevel = 'debug' | 'info' | 'warn' | 'error'

interface LoggerConfig {
type: LoggerType
level: LoggerLevel
consoleMethod: Console['log']
color: string
/**
Expand All @@ -17,42 +17,36 @@ interface LoggerConfig {

const configs: LoggerConfig[] = [
{
type: 'error',
consoleMethod: console.error,
color: '#DC143C',
colorCode: 160,
},
{
type: 'warn',
consoleMethod: console.warn,
color: '#FFA500',
colorCode: 214,
level: 'debug',
consoleMethod: console.debug,
color: 'gray',
colorCode: 7,
},
{
type: 'info',
level: 'info',
consoleMethod: console.info,
color: '#1E90FF',
colorCode: 27,
},
{
type: 'success',
consoleMethod: console.log,
color: '#32CD32',
colorCode: 40,
level: 'warn',
consoleMethod: console.warn,
color: '#FFA500',
colorCode: 214,
},
{
type: 'debug',
consoleMethod: console.debug,
color: 'gray',
colorCode: 7,
level: 'error',
consoleMethod: console.error,
color: '#DC143C',
colorCode: 160,
},
]

function createDebugger(
name: string,
{ type, consoleMethod, color, colorCode }: LoggerConfig,
{ level, consoleMethod, color, colorCode }: LoggerConfig,
) {
const logger = debug(`${name}:${type}`)
const logger = debug(`${name}:${level}`)
logger.log = consoleMethod.bind(console)
logger.color = isNode ? String(colorCode) : color
return logger
Expand All @@ -61,12 +55,12 @@ function createDebugger(
type LoggerMethod = (formatter: any, ...args: any[]) => void

function createLoggerMethods(name: string) {
return configs.map<[LoggerType, LoggerMethod]>(config =>
[config.type, createDebugger(name, config)],
return configs.map<[LoggerLevel, LoggerMethod]>(config =>
[config.level, createDebugger(name, config)],
)
}

type _Logger = Simplify<Record<LoggerType, LoggerMethod>>
type _Logger = Simplify<Record<LoggerLevel, LoggerMethod>>
interface Logger extends _Logger {
(title: string): _Logger
}
Expand All @@ -76,7 +70,7 @@ function createLogger(name: string) {
const _logger = Object.fromEntries(loggerMethods) as _Logger

const logger = (title: string) =>
Object.fromEntries(loggerMethods.map<[LoggerType, LoggerMethod]>(([type, method]) => [type, (formatter: any, ...args: any[]) => method(`[${title}] ${formatter}`, ...args)])) as _Logger
Object.fromEntries(loggerMethods.map<[LoggerLevel, LoggerMethod]>(([level, method]) => [level, (formatter: any, ...args: any[]) => method(`[${title}] ${formatter}`, ...args)])) as _Logger

Object.assign(logger, _logger)

Expand Down

0 comments on commit 939708d

Please sign in to comment.