-
Notifications
You must be signed in to change notification settings - Fork 19
add type script declaration #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { BaseLevels, BaseLogger } from 'anylogger' | ||
|
|
||
| interface Logger extends BaseLogger<LevelNames>, LevelConstants { | ||
| levels: Record<keyof BaseLevels, number>; | ||
| /** | ||
| * The log level of this logger. | ||
| */ | ||
| level: LogLevel; | ||
| assert(...args: any[]): void | ||
| dir(...args: any[]): void | ||
| table(...args: any[]): void | ||
| time(...args: any[]): void | ||
| timeEnd(...args: any[]): void | ||
| verbose(message?: any, ...args: any[]): void | ||
| silly(message?: any, ...args: any[]): void | ||
| discard(...args: any[]): void | ||
| } | ||
|
|
||
| type ValueOf<T> = T[keyof T] | ||
|
|
||
| type LevelNames = BaseLevels & { | ||
| assert: 0 | ||
| dir: 0 | ||
| table: 0 | ||
| time: 0 | ||
| timeEnd: 0 | ||
| verbose: 4 | ||
| silly: 6 | ||
| } | ||
|
|
||
| interface LevelConstants { | ||
| NONE: 0 | ||
| ERROR: 1 | ||
| WARN: 2 | ||
| INFO: 3 | ||
| LOG: 4 | ||
| DEBUG: 5 | ||
| TRACE: 6 | ||
| } | ||
|
|
||
| export type LogLevel = ValueOf<LevelConstants> | ||
|
|
||
| export type LogFormatter = (logger: Logger, method: keyof LevelNames, args: unknown[]) => void | ||
|
|
||
| export type LoggerExtender = (logger: Logger, parent: Logger) => void | ||
|
|
||
| export interface Middleware { | ||
| outputs: { | ||
| custom: { | ||
| log(...args: unknown[]): void; | ||
| }; | ||
| }; | ||
| use: LogFormatter[]; | ||
| formats: Record<string, (this: Logger, logger: Logger, method: keyof LevelNames, args: unknown[]) => void>; | ||
| formatters: { | ||
| custom(ctx: unknown): LogFormatter; | ||
| }; | ||
| } | ||
|
|
||
| declare namespace Logger { | ||
| function use(options: Partial<Middleware>): void; | ||
| } | ||
|
|
||
| export default Logger; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { LogFormatter } from '../'; | ||
| declare var formats: LogFormatter; | ||
| export default formats; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { Middleware } from '../' | ||
|
|
||
| declare var kurly: Middleware; | ||
| export default kurly; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { BaseLevels } from 'anylogger' | ||
| import anylogger from 'anylogger' | ||
| import ulog from 'ulog'; | ||
| import formats from 'ulog/mods/formats' | ||
| import kurly from 'ulog/mods/kurly' | ||
|
|
||
| const log = anylogger('my-app') as ulog; | ||
| log('Logging is easy!') | ||
|
|
||
| if (log.level >= log.INFO) { | ||
| log.info('This message will be logged') | ||
| } | ||
| log.level = log.WARN | ||
| log.info('This info message will NOT be logged.') | ||
| log.warn('This warning message WILL be logged.') | ||
| log.level = log.NONE | ||
| log.error('Logging is completely disabled.') | ||
|
|
||
| ulog.use({ | ||
| outputs: { | ||
| custom: { | ||
| log() { | ||
| var args = [].slice.call(arguments) | ||
| args.shift('Custom!!') | ||
| console.log.apply(console, args) | ||
| } | ||
| } | ||
| }, | ||
| use: [formats], | ||
| formats: { | ||
| cool(logger) { | ||
| // will be called on logger creation/extension | ||
| // replace the default log methods with formatters | ||
| // use bind so we leave the call stack intact. | ||
| // only works for static info like our 'Cool!' message | ||
| for (var level in this.levels) { | ||
| const lvl = level as keyof BaseLevels; | ||
| logger[lvl] = logger[lvl].bind(logger, 'Cool!') | ||
| } | ||
| // don't forget to format the method to discard to the drain as well! | ||
| logger.discard = logger.discard.bind(logger, 'Cool!') | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| ulog.use(kurly) | ||
| ulog.use({ | ||
| formatters: { | ||
| custom(ctx) { | ||
| return function (rec) { | ||
| return 'Custom!' | ||
| } | ||
| } | ||
| } | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "include": [ | ||
| "../**/*.ts" | ||
| ], | ||
| "compilerOptions": { | ||
| "module": "commonjs", | ||
| "lib": [ | ||
| "dom", | ||
| "es6" | ||
| ], | ||
| "noImplicitAny": true, | ||
| "noImplicitThis": true, | ||
| "strictNullChecks": true, | ||
| "strictFunctionTypes": true, | ||
| "baseUrl": "../../", | ||
| "typeRoots": [ | ||
| "../../" | ||
| ], | ||
| "types": [], | ||
| "noEmit": true, | ||
| "forceConsistentCasingInFileNames": true | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, I add a method to Logger. you can add mods as well: