-
Notifications
You must be signed in to change notification settings - Fork 35
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
WIP: feat: first implement logger #143
Open
popomore
wants to merge
1
commit into
master
Choose a base branch
from
logger
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 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
This file contains 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 @@ | ||
# Change Log |
This file contains 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 @@ | ||
# `@eggjs/logger-decorator` | ||
|
||
# Usage |
This file contains 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 @@ | ||
export * from './src/decorator/LoggerDecorator'; | ||
export * from './src/logger/BaseLogger'; | ||
export * from './src/logger/LoggerLevel'; |
This file contains 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,54 @@ | ||
{ | ||
"name": "@eggjs/logger", | ||
"version": "3.14.3", | ||
"description": "tegg logger", | ||
"keywords": [ | ||
"tegg", | ||
"logger", | ||
"typescript", | ||
"egg" | ||
], | ||
"author": "popomore <sakura9515@gmail.com>", | ||
"homepage": "https://github.com/eggjs/tegg", | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:eggjs/tegg.git", | ||
"directory": "core/logger" | ||
}, | ||
"dependencies": { | ||
"@eggjs/core-decorator": "^3.14.3", | ||
"@eggjs/tegg-common-util": "^3.14.3", | ||
"@eggjs/tegg-metadata": "^3.14.3", | ||
"is-type-of": "^2.0.1" | ||
}, | ||
"scripts": { | ||
"test": "cross-env NODE_ENV=test NODE_OPTIONS='--no-deprecation' mocha", | ||
"clean": "tsc -b --clean", | ||
"tsc": "npm run clean && tsc -p ./tsconfig.json", | ||
"tsc:pub": "npm run clean && tsc -p ./tsconfig.pub.json", | ||
"prepublishOnly": "npm run tsc:pub" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"engines": { | ||
"node": ">=14.0.0" | ||
}, | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"files": [ | ||
"dist/**/*.js", | ||
"dist/**/*.d.ts" | ||
], | ||
"bugs": { | ||
"url": "https://github.com/eggjs/tegg/issues" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "^10.0.1", | ||
"@types/node": "^20.2.4", | ||
"cross-env": "^7.0.3", | ||
"mocha": "^10.2.0", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.0.4" | ||
} | ||
} |
This file contains 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 @@ | ||
import { AccessLevel, EggProtoImplClass, PrototypeUtil, SingletonProto } from '@eggjs/core-decorator'; | ||
import { StackUtil } from '@eggjs/tegg-common-util'; | ||
import { LoggerMetaUtil } from '../logger/LoggerUtil'; | ||
import { ILogger } from '../type/ILogger'; | ||
import { LoggerDecoratorParams } from '../type/LoggerDecoratorParams'; | ||
|
||
/** | ||
* Logger Decorator | ||
* | ||
* Define the object is Logger object | ||
*/ | ||
export function LoggerDecorator(params?: LoggerDecoratorParams) { | ||
return function(constructor: EggProtoImplClass<ILogger>) { | ||
LoggerMetaUtil.setLoggerFlag(constructor); | ||
LoggerMetaUtil.setLoggerMeta(constructor, params); | ||
const func = SingletonProto({ | ||
accessLevel: AccessLevel.PRIVATE, | ||
name: params?.name, | ||
}); | ||
func(constructor); | ||
PrototypeUtil.setFilePath(constructor, StackUtil.getCalleeFromStack(false, 5)); | ||
}; | ||
} |
This file contains 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,35 @@ | ||
import { Logger } from 'egg-logger'; | ||
import { LoggerFactory } from './LoggerFactory'; | ||
import { ILogger } from '../type/ILogger'; | ||
import { LoggerMetaUtil } from './LoggerUtil'; | ||
|
||
export class BaseLogger implements ILogger { | ||
|
||
#logger: Logger; | ||
|
||
constructor() { | ||
const options = LoggerMetaUtil.getLoggerMeta(this.constructor as any); | ||
this.#logger = LoggerFactory.getOrCreateLogger(options); | ||
} | ||
|
||
public debug(message: any, ...args: any[]): void { | ||
this.#logger.debug(message, ...args); | ||
} | ||
|
||
public error(message: any, ...args: any[]): void { | ||
this.#logger.error(message, ...args); | ||
} | ||
|
||
public info(message: any, ...args: any[]): void { | ||
this.#logger.error(message, ...args); | ||
} | ||
|
||
public warn(message: any, ...args: any[]): void { | ||
this.#logger.warn(message, ...args); | ||
} | ||
|
||
public write(message: any): void { | ||
this.#logger.write(message); | ||
} | ||
|
||
} |
This file contains 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,43 @@ | ||
import { Logger } from 'egg-logger'; | ||
import is from 'is-type-of'; | ||
import { LoggerDecoratorParams } from '../type/LoggerDecoratorParams'; | ||
import { LoggerLevel } from './LoggerLevel'; | ||
|
||
const defaultOptions: Partial<LoggerDecoratorParams> = { | ||
level: LoggerLevel.INFO, | ||
}; | ||
|
||
export class LoggerFactory { | ||
|
||
static #loggerMap = new Map<string, Logger>(); | ||
|
||
static getOrCreateLogger(options?: LoggerDecoratorParams): Logger { | ||
const opt = { | ||
...defaultOptions, | ||
...options, | ||
}; | ||
|
||
if (is.nullable(opt.file)) throw new Error('options.file is required'); | ||
if (is.nullable(opt.name)) throw new Error('options.name is required'); | ||
|
||
const name = opt.name; | ||
let logger = this.#loggerMap.get(name); | ||
if (logger) return logger; | ||
|
||
logger = new Logger({ | ||
level: opt.level, | ||
file: opt.file, | ||
}); | ||
this.#loggerMap.set(name, logger); | ||
return logger; | ||
} | ||
|
||
static getLogger(name: string): Logger | undefined { | ||
return this.#loggerMap.get(name); | ||
} | ||
|
||
// static destroyAll() { | ||
|
||
// } | ||
|
||
} |
This file contains 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,7 @@ | ||
export enum LoggerLevel { | ||
DEBUG = 'DEBUG', | ||
INFO = 'INFO', | ||
WARN = 'WARN', | ||
ERROR = 'ERROR', | ||
NONE = 'NONE', | ||
} |
This file contains 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,27 @@ | ||
import { EggProtoImplClass, MetadataUtil } from '@eggjs/core-decorator'; | ||
import { LoggerDecoratorParams } from '../type/LoggerDecoratorParams'; | ||
import { ILogger } from '../type/ILogger'; | ||
import is from 'is-type-of'; | ||
|
||
export const LOGGER_OPTIONS = Symbol.for('EggPrototype#loggerOptions'); | ||
export const IS_LOGGER = Symbol.for('EggPrototype#isLogger'); | ||
|
||
export class LoggerMetaUtil { | ||
static setLoggerMeta(clazz: EggProtoImplClass<ILogger>, params?: LoggerDecoratorParams) { | ||
MetadataUtil.defineMetaData(LOGGER_OPTIONS, params, clazz); | ||
} | ||
|
||
static getLoggerMeta(clazz: EggProtoImplClass<ILogger>): LoggerDecoratorParams | undefined { | ||
return MetadataUtil.getOwnMetaData(LOGGER_OPTIONS, clazz); | ||
} | ||
|
||
static setLoggerFlag(clazz: EggProtoImplClass<ILogger>) { | ||
MetadataUtil.defineMetaData(IS_LOGGER, true, clazz); | ||
} | ||
|
||
static getLoggerFlag(clazz: EggProtoImplClass<ILogger>): boolean { | ||
const result = MetadataUtil.getOwnMetaData<boolean>(IS_LOGGER, clazz); | ||
return is.undefined(result) ? false : result; | ||
} | ||
|
||
} |
This file contains 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,10 @@ | ||
import { LoggerDecorator } from '../decorator/LoggerDecorator'; | ||
import { BaseLogger } from '../logger/BaseLogger'; | ||
|
||
@LoggerDecorator({ | ||
name: 'logger', | ||
transports: [ | ||
new (require('egg-logger').EggConsoleTransport)({ | ||
], | ||
}) | ||
export class Logger extends BaseLogger {} |
This file contains 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,7 @@ | ||
export interface ILogger { | ||
error(message: any, ...args: any[]): void; | ||
warn(message: any, ...args: any[]): void; | ||
info(message: any, ...args: any[]): void; | ||
debug(message: any, ...args: any[]): void; | ||
write(message: any, ...args: any[]): void; | ||
} |
This file contains 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,17 @@ | ||
import { LoggerLevel } from '../logger/LoggerLevel'; | ||
|
||
export interface LoggerDecoratorParams { | ||
name?: string; | ||
level?: LoggerLevel; | ||
file: string; | ||
// encoding?: string; | ||
// consoleLevel?: LoggerLevel; | ||
// formatter?: (meta?: LoggerMeta) => string; | ||
// paddingMessageFormatter?: (ctx: object) => string; | ||
// jsonFile?: string; | ||
// outputJSON?: boolean; | ||
// buffer?: boolean; | ||
// dateISOFormat?: boolean; | ||
// eol?: string; | ||
// concentrateError?: 'duplicate' | 'redirect' | 'ignore'; | ||
} |
This file contains 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,43 @@ | ||
import path from 'path'; | ||
import { LoadUnitInstance, LoadUnitInstanceFactory } from '@eggjs/tegg-runtime'; | ||
import { LoadUnitFactory } from '@eggjs/tegg-metadata'; | ||
import { EggTestContext } from '../../test-util'; | ||
import { HelloService } from './fixtures/modules/dynamic-inject-module/HelloService'; | ||
import { CoreTestHelper } from '../../test-util/CoreTestHelper'; | ||
import assert from 'assert'; | ||
import { TraceLogger } from './fixtures/logger/TraceLogger'; | ||
|
||
describe('test/Logger.test.ts', () => { | ||
|
||
|
||
let modules: Array<LoadUnitInstance>; | ||
beforeEach(async () => { | ||
modules = await CoreTestHelper.prepareModules([ | ||
path.join(__dirname, '..'), | ||
path.join(__dirname, 'fixtures/logger'), | ||
]); | ||
}); | ||
|
||
afterEach(async () => { | ||
for (const module of modules) { | ||
await LoadUnitFactory.destroyLoadUnit(module.loadUnit); | ||
await LoadUnitInstanceFactory.destroyLoadUnitInstance(module); | ||
} | ||
}); | ||
|
||
describe('Pointcut', () => { | ||
it('should work', async () => { | ||
const traceLogger = await CoreTestHelper.getObject(TraceLogger); | ||
traceLogger.info('info %s', ); | ||
}); | ||
}); | ||
|
||
// describe('Logger Define', () => { | ||
|
||
// }); | ||
|
||
// describe('Logger Manager', () => { | ||
|
||
// }); | ||
|
||
}); |
This file contains 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,6 @@ | ||
import { BaseLogger, LoggerDecorator } from '../../..'; | ||
|
||
@LoggerDecorator({ | ||
file: 'trace.log', | ||
}) | ||
export class TraceLogger extends BaseLogger{} |
This file contains 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,12 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"baseUrl": "./" | ||
}, | ||
"exclude": [ | ||
"dist", | ||
"node_modules", | ||
"test" | ||
] | ||
} |
This file contains 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,12 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"baseUrl": "./" | ||
}, | ||
"exclude": [ | ||
"dist", | ||
"node_modules", | ||
"test" | ||
] | ||
} |
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.
ILogger 是不是单独发一个 npm 包?像 layotto js-sdk 其实不感知 tegg 的存在的。
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.
那是不是把 zlogger 这个库改造下,把 egg-logger 的一些实现放过去
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.
最好就是纯 interface 定义,这样依赖都可以不添加。https://github.com/layotto/js-sdk/blob/main/src/client/Client.ts#L79
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.
对啊,正常还是可以依赖一个 logger 的,现在只是没有承载。