Skip to content
This repository has been archived by the owner on Sep 30, 2022. It is now read-only.

Commit

Permalink
refactor: allow lowercase log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed Sep 15, 2021
1 parent 82380e3 commit 2de2caf
Show file tree
Hide file tree
Showing 21 changed files with 269 additions and 63 deletions.
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,65 +53,65 @@ log('debug log')
Create a log entry with a red cross.

```typescript
import log from '@flex-development/log'
import log, { LogLevel } from '@flex-development/log'

/**
* @file Examples - error log
* @module log/docs/examples/error
*/

log('error log', { level: 'ERROR' })
log('error log', { level: LogLevel.ERROR })
```

#### Info

Create a log entry with a blue info symbol.

```typescript
import log from '@flex-development/log'
import log, { LogLevel } from '@flex-development/log'

/**
* @file Examples - info log
* @module log/docs/examples/info
*/

log('info log', { level: 'INFO' })
log('info log', { level: LogLevel.INFO })
```

#### Success

Create a log entry with a green tick mark.

```typescript
import log from '@flex-development/log'
import log, { LogLevel } from '@flex-development/log'

/**
* @file Examples - success log
* @module log/docs/examples/success
*/

log('success log', { level: 'SUCCESS' })
log('success log', { level: LogLevel.SUCCESS })
```

#### Warning

Create a log entry with a yellow exclamation point.

```typescript
import log from '@flex-development/log'
import log, { LogLevel } from '@flex-development/log'

/**
* @file Examples - warning log
* @module log/docs/examples/warning
*/

log('warning log', { level: 'WARN' })
log('warning log', { level: LogLevel.WARN })
```

### Options

```typescript
export interface LogOptions {
interface LogOptions {
/**
* Log arguments.
*
Expand Down Expand Up @@ -159,7 +159,15 @@ export interface LogOptions {
silent?: boolean
}

type Level = 'DEBUG' | 'ERROR' | 'INFO' | 'SUCCESS' | 'WARN'
enum LogLevel {
DEBUG = 'debug',
ERROR = 'error',
INFO = 'info',
SUCCESS = 'success',
WARN = 'warn'
}

export type Level = keyof typeof LogLevel | LogLevel
```
## Built With
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/error.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import log from '@flex-development/log'
import log, { LogLevel } from '@flex-development/log'

/**
* @file Examples - error log
* @module log/docs/examples/error
*/

log('error log', { level: 'ERROR' })
log('error log', { level: LogLevel.ERROR })
4 changes: 2 additions & 2 deletions docs/examples/info.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import log from '@flex-development/log'
import log, { LogLevel } from '@flex-development/log'

/**
* @file Examples - info log
* @module log/docs/examples/info
*/

log('info log', { level: 'INFO' })
log('info log', { level: LogLevel.INFO })
4 changes: 2 additions & 2 deletions docs/examples/success.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import log from '@flex-development/log'
import log, { LogLevel } from '@flex-development/log'

/**
* @file Examples - success log
* @module log/docs/examples/success
*/

log('success log', { level: 'SUCCESS' })
log('success log', { level: LogLevel.SUCCESS })
4 changes: 2 additions & 2 deletions docs/examples/warning.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import log from '@flex-development/log'
import log, { LogLevel } from '@flex-development/log'

/**
* @file Examples - warning log
* @module log/docs/examples/warning
*/

log('warning log', { level: 'WARN' })
log('warning log', { level: LogLevel.WARN })
15 changes: 8 additions & 7 deletions src/__tests__/log.functional.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import defaults from '@log/config/defaults.config'
import type { LogOptions } from '@log/interfaces'
import format from '@log/utils/format.util'
import normalizeOptions from '@log/utils/normalize-options.util'
import type { RestoreConsole } from 'jest-mock-console'
import mockConsole from 'jest-mock-console'
import merge from 'lodash.merge'
import sh from 'shelljs'
import testSubject from '../log'

Expand All @@ -13,28 +13,29 @@ import testSubject from '../log'
*/

jest.mock('@log/utils/format.util')
jest.mock('@log/utils/normalize-options.util')

const mockSH = sh as jest.Mocked<typeof sh>
const mockFormat = format as jest.MockedFunction<typeof format>
const mockMerge = merge as jest.MockedFunction<typeof merge>
const mockNormalizeOptions = normalizeOptions as jest.MockedFunction<
typeof normalizeOptions
>

describe('functional:log', () => {
const restoreConsole: RestoreConsole = mockConsole(['log'])
const spy_console_log = jest.spyOn(console, 'log')

afterAll(() => restoreConsole())

it('should merge options with defaults', () => {
it('should normalize options', () => {
// Arrange
const options: LogOptions = { level: 'ERROR' }

// Act
testSubject('', options)

// Expect
expect(mockMerge).toBeCalledTimes(1)
expect(mockMerge.mock.calls[0][1]).toStrictEqual(defaults)
expect(mockMerge.mock.calls[0][2]).toStrictEqual(options)
expect(mockNormalizeOptions.mock.calls[0][0]).toMatchObject(options)
})

it('should format log entry', () => {
Expand All @@ -47,7 +48,7 @@ describe('functional:log', () => {

// Expect
expect(mockFormat).toBeCalledTimes(1)
expect(mockFormat).toBeCalledWith(data, options)
expect(mockFormat).toBeCalledWith(data, mockNormalizeOptions(options))
})

describe('options', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/config/defaults.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LogLevel } from '@log/enums/log-level.enum'
import type { LogOptions } from '@log/interfaces'

/**
Expand All @@ -9,5 +10,5 @@ export default Object.freeze({
args: [],
bold: { args: true, data: false },
color: {},
level: 'DEBUG'
level: LogLevel.DEBUG
}) as Readonly<Required<Pick<LogOptions, 'args' | 'bold' | 'color' | 'level'>>>
17 changes: 9 additions & 8 deletions src/enums/log-figure.enum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import figures from 'figures'
import type { Level } from './log-level.enum'
import type { LogLevel } from './log-level.enum'

/**
* @file Enums - LogFigure
Expand All @@ -14,10 +14,11 @@ import type { Level } from './log-level.enum'
* @readonly
* @enum {string}
*/
export const LogFigure: Readonly<Record<Level, string>> = Object.freeze({
DEBUG: '',
ERROR: figures.cross,
INFO: figures.info,
SUCCESS: figures.tick,
WARN: '!'
})
export const LogFigure: Readonly<Record<keyof typeof LogLevel, string>> =
Object.freeze({
DEBUG: '',
ERROR: figures.cross,
INFO: figures.info,
SUCCESS: figures.tick,
WARN: '!'
})
4 changes: 1 addition & 3 deletions src/enums/log-level.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@ export enum LogLevel {
ERROR = 'error',
INFO = 'info',
SUCCESS = 'success',
WARN = 'warning'
WARN = 'warn'
}

export type Level = keyof typeof LogLevel
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ import log from './log'
* @module log
*/

export { LogLevel } from './enums/log-level.enum'
export type { LogOptions } from './interfaces'
export type { Level } from './types'

export default log
4 changes: 2 additions & 2 deletions src/interfaces/log-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NullishString } from '@flex-development/tutils'
import type { Level } from '@log/enums/log-level.enum'
import type { Level } from '@log/types'
import figures from 'figures'
import type { LogOptionsBold } from './log-options-bold.interface'
import type { LogOptionsColor } from './log-options-color.interface'
Expand Down Expand Up @@ -42,7 +42,7 @@ export interface LogOptions {
/**
* Log level.
*
* @default 'DEBUG'
* @default LogLevel.DEBUG
*/
level?: Level

Expand Down
8 changes: 4 additions & 4 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { NullishString } from '@flex-development/tutils'
import defaults from '@log/config/defaults.config'
import type { Level } from '@log/enums/log-level.enum'
import type {
LogOptions,
LogOptionsBold,
LogOptionsColor
} from '@log/interfaces'
import type { Level } from '@log/types'
import type { Color } from 'chalk'
import figs from 'figures'
import merge from 'lodash.merge'
import format from './utils/format.util'
import normalizeOptions from './utils/normalize-options.util'

/**
* @file Log Method
Expand All @@ -34,14 +34,14 @@ import format from './utils/format.util'
* @param {typeof Color} [options.color.data] - Set log data color
* @param {typeof Color} [options.color.figure] - Set log figure color
* @param {keyof typeof figs | NullishString} [options.figure] - Override figure
* @param {Level} [options.level='DEBUG'] - Log level
* @param {Level} [options.level=LogLevel.DEBUG] - Log level
* @param {boolean} [options.shell] - Use [`echo`][2] instead of `console.log`
* @param {boolean} [options.silent] - Do not log any output
* @return {string} Formatted log entry
*/
const log = (data: any, options: LogOptions = defaults): string => {
// Merge options with defaults
const $options = merge({}, defaults, options)
const $options = normalizeOptions(options)

// Create formatted log entry
const entry = format(data, $options)
Expand Down
11 changes: 11 additions & 0 deletions src/types/Level.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { LogLevel } from '@log/enums/log-level.enum'

/**
* @file Type Definitions - Level
* @module log/types/Level
*/

/**
* Log levels.
*/
export type Level = keyof typeof LogLevel | LogLevel
6 changes: 6 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @file Entry Point - Type Definitions
* @module log/types
*/

export type { Level } from './Level'
9 changes: 9 additions & 0 deletions src/utils/__mocks__/normalize-options.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @file User Module Mock - normalizeOptions
* @module utils/mocks/normalizeOptions
* @see https://jestjs.io/docs/manual-mocks#mocking-user-modules
*/

export default jest.fn((...args) => {
return jest.requireActual('../normalize-options.util').default(...args)
})
Loading

0 comments on commit 2de2caf

Please sign in to comment.