-
Notifications
You must be signed in to change notification settings - Fork 359
Execution logger #83
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
Execution logger #83
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
03e4e01
第一次初始化提交
1579923417 c97a424
初始化
1579923417 46902a0
1. 按照建议修改代码完毕
1579923417 49892fc
Refactoring logging: using log.ts for unified logging
1579923417 b073166
ignore `pnpm-lock.yaml`
1579923417 932761f
feat:Optimize the logging system and simplify timestamp generation logic
1579923417 f555c06
Merge remote-tracking branch 'upstream/develop' into execution-logger
1579923417 3c679e9
Feature: -The method of replacing all console logging instances with …
1579923417 de6e80c
feat: replace custom logger with loglevel
1579923417 8561895
feat: add log storage and improve recorder instance retrieval
1579923417 159d4ef
Fix - Update EkoLoger to EkoLogerFactory
1579923417 f0f56d3
Merge remote-tracking branch 'upstream/develop' into execution-logger
1579923417 2afde48
Merge remote-tracking branch 'origin/develop' into execution-logger
1579923417 8e2eeb4
Merge remote-tracking branch 'origin/develop' into execution-logger
1579923417 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
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
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 |
---|---|---|
@@ -1,47 +1,43 @@ | ||
import { Logger, ILogObj } from 'tslog'; | ||
import { join } from 'path'; | ||
import { tmpdir } from 'os'; | ||
import log from 'loglevel'; | ||
|
||
export class EkoLogger { | ||
private logger: Logger<ILogObj>; | ||
private logFilePath: string; | ||
private logger: log.Logger; | ||
private logStorage: string[] = []; | ||
|
||
constructor() { | ||
const now = new Date(); | ||
const timestamp = now.toISOString().replace(/[-:.TZ]/g, '').slice(0, 14); | ||
|
||
const logFileName = `Eko-${timestamp}.log`; | ||
this.logFilePath = join(tmpdir(), logFileName); | ||
|
||
this.logger = new Logger({ | ||
name: "EkoLog", | ||
overwrite: { | ||
mask: (...args: any[]): unknown[] => { | ||
return args.map((arg) => { | ||
return this.toReadableString(arg); | ||
}); | ||
}, | ||
}, | ||
}); | ||
|
||
// log file path | ||
this.logger.info(`log file path at: ${this.logFilePath}`); | ||
} | ||
this.logger = log.getLogger('EkoLogger'); | ||
this.logger.setLevel(log.levels.TRACE); | ||
|
||
const originalFactory = this.logger.methodFactory; | ||
|
||
this.logger.methodFactory = (methodName, logLevel, loggerName) => { | ||
const rawMethod = originalFactory(methodName, logLevel, loggerName); | ||
return (...args: any[]) => { | ||
const truncatedArgs = args.map(arg => this.toReadableString(arg)); | ||
this.logStorage.push(`[${methodName.toUpperCase()}] ${truncatedArgs.join(' ')}`); | ||
rawMethod(...truncatedArgs); | ||
}; | ||
}; | ||
|
||
} | ||
// truncate content if it exceeds 2048 characters | ||
private toReadableString(content: any): string { | ||
const contentString = JSON.stringify(content); | ||
const maxLength = 2048; | ||
if (contentString.length > maxLength) { | ||
return contentString.substring(0, maxLength) + '...'; | ||
return contentString.substring(0, maxLength - 3) + '...'; | ||
} else { | ||
return contentString; | ||
} | ||
} | ||
|
||
public getLogger(): Logger<ILogObj> { | ||
public getLoggerInstance(): log.Logger { | ||
return this.logger; | ||
} | ||
|
||
public getAllLogs(): string[] { | ||
return this.logStorage; | ||
} | ||
} | ||
|
||
export const logger = new EkoLogger().getLogger(); | ||
export const logger = new EkoLogger().getLoggerInstance(); | ||
HairlessVillager marked this conversation as resolved.
Show resolved
Hide resolved
|
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
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
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
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
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
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
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
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
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
1579923417 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file was deleted.
Oops, something went wrong.
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
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.
潜在的性能瓶颈?当大量日志涌入时,这里的性能会不会过慢而拖慢整个工作流的后腿?问一下 AI。
Uh oh!
There was an error while loading. Please reload this page.
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.
在高并发或大量日志的情况下,这段代码确实会存在性能瓶颈。
1. 性能瓶颈分析
(1)日志存储(
logStorage
)logStorage
是一个数组,用于存储所有日志内容。随着日志数量的增加,logStorage
的大小会不断增长,这可能导致以下问题:logStorage.push()
时,数组会动态扩展,这在 JavaScript 中可能涉及内存重新分配。当数组非常大时,频繁的内存分配和扩展会显著降低性能。(2)日志内容截断和序列化
在
toReadableString()
方法中,对日志内容进行了截断和序列化:JSON.stringify()
是一个相对较慢的操作,尤其是对于复杂对象。在高并发场景下,大量调用JSON.stringify()
可能会拖慢性能。toReadableString
的方法不能很好地处理嵌套对象,无法达到预期的效果(没有进行截断),目前修改了几版代码还是无法解决这个问题。2. 优化建议
限制日志存储大小
logStorage
设计为一个固定大小的存储结构,例如使用环形缓冲区(FIFO 队列)。当存储满时,自动丢弃最早的日志。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.
我让你问 AI 不是因为我懒得问,是因为我想让你参考 AI 的回复来决定是否修改这里的实现……