Skip to content

Commit

Permalink
Prepare for first release (#3)
Browse files Browse the repository at this point in the history
* Remove console.log statement in generateThemeTypes.ts

* Update package version and author information

* Fix testing timezone bug

* Improve documentation
  • Loading branch information
CasperSocio committed Dec 14, 2023
1 parent 857b49c commit 9449c7c
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 28 deletions.
128 changes: 128 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,131 @@ This repository contains the Logger class, a utility for managing and displaying
## Getting started

Run `npm i -D @socio-development/logger`

### Example

```ts
import { Logger } from '@socio-development/logger'

const log = new Logger()

log.group('Process: START')
log.add('This is my default log')
log.info('This is my info log')
log.add('This is my default log')
log.warn('This is my warn log')
log.add('This is my default log')
log.error('This is my error log')
log.groupEnd('Process: END')

log.print()
```

> [Insert screenshot here]
The `Logger` class is a custom logging utility that provides methods for logging messages with different severity levels and grouping them for better readability.

The script begins by creating a new instance of the `Logger` class:

```ts
const log = new Logger()
```

Next, it uses the `group` method to start a new group of log entries:

```ts
log.group('Process: START')
```

The `group` method logs the provided message and increases the current indentation level. This means that all log entries added after this point will be indented until the group is ended.

The script then uses the `add`, `info`, `warn`, and `error` methods to log messages with different severity levels:

```ts
log.add('This is my default log')
log.info('This is my info log')
log.warn('This is my warn log')
log.error('This is my error log')
```

Each of these methods creates a new `LogEntry` with the provided message, the current indentation level, and the appropriate severity level, and adds it to the list of log entries.

Finally, the script uses the `groupEnd` method to end the current group of log entries and the `print` method to print all log entries to the console:

```ts
log.groupEnd('Process: END')
log.print()
```

The `groupEnd` method decreases the current indentation level and logs the provided message, if any. The `print` method formats all log entries according to the logger's configuration and prints them to the console, separated by newline characters.

# Options

You can change the logger output by providing options to the logger.

## Composition

**Name** `.composition`

**Default**
```ts
LoggerOptions.composition.default = [
'timestamp',
'prefix',
'indent',
'message'
]

LoggerOptions.composition.error = [
'color:red',
'timestamp',
'prefix',
'indent',
'message',
'color:reset',
]

LoggerOptions.composition.info = [
'color:blue',
'timestamp',
'prefix',
'indent',
'message',
'color:reset',
]

LoggerOptions.composition.warn = [
'color:yellow',
'timestamp',
'prefix',
'indent',
'message',
'color:reset',
]
```

The composition option allows you to modify the logger output for each severity level.

## Indent size

**Name** `.indentSize`

**Default** `2`

## Prefix

**Name** `.prefix`

**Default** `undefined`

## Separator

**Name** `.separator`

**Default** `' '`

## Timestamp format

**Name** `.timestampFormat`

**Default** `'HH:mm:ss'`
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 11 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
{
"name": "@socio-development/logger",
"version": "0.1.0-pre-alpha",
"version": "0.1.0-alpha.0",
"description": "A logging utility for managing and displaying logs in a structured manner.",
"author": "CasperSocio",
"author": "CasperSocio (https://github.com/CasperSocio)",
"scripts": {
"build": "npm run generate:theme && tsc",
"build": "rm -rf dist && npm run generate:theme && tsc",
"dev": "ts-node src/sandbox.ts",
"generate:theme": "ts-node scripts/generateThemeTypes.ts",
"prepare": "husky install",
"test": "jest",
"test:ci": "jest --ci --coverage=false",
"test:watch": "jest --watch"
"test": "TZ=UTC jest",
"test:ci": "TZ=UTC jest --ci --coverage=false",
"test:watch": "TZ=UTC jest --watch"
},
"files": [
"dist"
],
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.js"
}
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": "./dist/index.js",
"./package.json": "./package.json"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Socio-Development/logger.git"
Expand All @@ -32,8 +30,7 @@
"url": "https://github.com/Socio-Development/logger/issues"
},
"keywords": [
"logger",
"node"
"logger"
],
"license": "MIT",
"devDependencies": {
Expand Down
2 changes: 0 additions & 2 deletions scripts/generateThemeTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export type ThemeName = (typeof themeNames)[number]
`

console.log(code)

generate({
code,
file: 'theme.ts',
Expand Down
16 changes: 8 additions & 8 deletions src/__tests__/classes/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Logger', () => {

logger.add('test')

expect(logger.formattedEntries).toEqual(['17:46:28 test'])
expect(logger.formattedEntries).toEqual(['16:46:28 test'])

global.Date = OriginalDate
})
Expand All @@ -60,7 +60,7 @@ describe('Logger', () => {

logger.add('test')

expect(logger['_formatEntry'](logger.entries[0])).toEqual('17:46:28 test')
expect(logger['_formatEntry'](logger.entries[0])).toEqual('16:46:28 test')
})

it('should correctly apply indentation', () => {
Expand All @@ -70,11 +70,11 @@ describe('Logger', () => {
logger.add('test')
logger.groupEnd('test')

expect(logger['_formatEntry'](logger.entries[0])).toEqual('17:46:28 test')
expect(logger['_formatEntry'](logger.entries[0])).toEqual('16:46:28 test')
expect(logger['_formatEntry'](logger.entries[1])).toEqual(
'17:46:28 test',
'16:46:28 test',
)
expect(logger['_formatEntry'](logger.entries[2])).toEqual('17:46:28 test')
expect(logger['_formatEntry'](logger.entries[2])).toEqual('16:46:28 test')
})

it('should be able to handle a prefix', () => {
Expand All @@ -83,7 +83,7 @@ describe('Logger', () => {
logger.add('test')

expect(logger['_formatEntry'](logger.entries[0])).toEqual(
'17:46:28 [TEST] test',
'16:46:28 [TEST] test',
)
})

Expand All @@ -93,7 +93,7 @@ describe('Logger', () => {
logger.info('test')

expect(logger['_formatEntry'](logger.entries[0])).toEqual(
'\x1b[34m17:46:28 test\x1b[0m',
'\x1b[34m16:46:28 test\x1b[0m',
)
})
})
Expand Down Expand Up @@ -195,7 +195,7 @@ describe('Logger', () => {

logger.print()

expect(spy).toHaveBeenCalledWith('17:46:28 test')
expect(spy).toHaveBeenCalledWith('16:46:28 test')

global.Date = OriginalDate
})
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/classes/Timestamp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Timestamp', () => {

expect(timestamp.format('YYYY-MM-DD')).toBe('2022-02-17')
expect(timestamp.format('YYYY-MM-DD HH:mm:ss A')).toBe(
'2022-02-17 17:46:28 PM',
'2022-02-17 16:46:28 PM',
)
})
it('should apply AM/PM', () => {
Expand All @@ -53,7 +53,7 @@ describe('Timestamp', () => {
const timestamp = new Timestamp()

expect(timestamp.format('YYYY-MM-DD HH:mm:ss A')).toBe(
'2022-02-17 07:46:28 AM',
'2022-02-17 06:46:28 AM',
)

global.Date = jest.fn(() => mockDate) as any
Expand Down

0 comments on commit 9449c7c

Please sign in to comment.