Skip to content

Commit

Permalink
Merge pull request #101 from AliMD/feat/logger
Browse files Browse the repository at this point in the history
[Logger] Add debug property and improve documents
  • Loading branch information
alimd authored Mar 12, 2022
2 parents 5910ed9 + 8f83d29 commit 0dc2b67
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
39 changes: 29 additions & 10 deletions package/logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,33 @@ Please remember to **reload** the window after changing the debug mode.

### API

### Prepare the logger
### `createLogger(scope: string, color: string, force = boolean)`

Create a logger function for fancy console debug with custom scope.

- **color** is optional and automatically select from internal fancy color list.
- **force** is optional and default to false.

Example:

```ts
import {createLogger} from 'https://esm.run/@alwatr/logger';
const logger = createLogger('logger/demo', 'green');
const logger = createLogger('logger/demo');
```

### `logProperty(property, value)`
### `logger.debug: boolean`

Debug state for current scope base on localStorage `ALWATR_LOG` pattern.

### `logger.color: string`

Debug state for current scope base on localStorage `ALWATR_LOG` pattern.

### `logger.scope: string`

Debug state for current scope base on localStorage `ALWATR_LOG` pattern.

### `logger.logProperty(property, value)`

`console.debug` property change.

Expand All @@ -56,7 +75,7 @@ Example:
logger.logProperty('name', 'ali');
```

### `logMethod(method)`
### `logger.logMethod(method)`

`console.debug` function or method calls.

Expand All @@ -68,7 +87,7 @@ function myMethod () {
}
```

### `logMethodArgs(method, args)`
### `logger.logMethodArgs(method, args)`

`console.debug` function or method calls with arguments.

Expand All @@ -80,7 +99,7 @@ function myMethod (a: number, b: number) {
}
```

### `logMethodFull(method, args, result)`
### `logger.logMethodFull(method, args, result)`

`console.debug` function or method calls with arguments.

Expand All @@ -94,7 +113,7 @@ function add (a: number, b: number): number {
}
```

### `incident(method, code, description, ...args)`
### `logger.incident(method, code, description, ...args)`

`console.trace` an event or expected accident. (not warn or error)

Expand All @@ -104,7 +123,7 @@ Example:
logger.incident('fetch', 'abort_signal', 'aborted signal received', {url: '/test.json'});
```

### `accident(method, code, description, ...args)`
### `logger.accident(method, code, description, ...args)`

`console.warn` an unexpected accident or error that you handled.

Expand All @@ -114,7 +133,7 @@ Example:
logger.accident('fetch', 'file_not_found', 'url requested return 404 not found', {url: '/test.json'});
```

### `error(method, code, errorStack, ...args)`
### `logger.error(method, code, errorStack, ...args)`

`console.error` an unexpected error.

Expand All @@ -129,7 +148,7 @@ catch (err) {
}
```

### `logOther(...args)`
### `logger.logOther(...args)`

Simple `console.debug` with styled scope.

Expand Down
15 changes: 10 additions & 5 deletions package/logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,15 @@ export const style = {
/**
* Create a logger function for fancy console debug with custom scope.
*
* @example
* const logger = createLogger('demo');
* function sayHello (name: string) {
* logger.logMethodArgs('sayHello', {name});
* }
* - **color** is optional and automatically select from internal fancy color list.
* - **force** is optional and default to false.
*
* Example:
*
* ```ts
* import {createLogger} from 'https://esm.run/@alwatr/logger';
* const logger = createLogger('logger/demo');
* ```
*/
export const createLogger = (
scope: string,
Expand All @@ -108,6 +112,7 @@ export const createLogger = (
* Required logger object, accident, error always reported even when the debug is false.
*/
const requiredItems = {
debug,
color,
scope,

Expand Down
11 changes: 11 additions & 0 deletions package/logger/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@ declare global {
}

export interface Logger {
/**
* Debug state for current scope base on localStorage `ALWATR_LOG` pattern.
*/
readonly debug: boolean;

/**
* Color picked for current scope.
*/
readonly color: string;

/**
* Scope defined for this logger.
*/
readonly scope: string;

/**
Expand Down

0 comments on commit 0dc2b67

Please sign in to comment.