Skip to content

Commit

Permalink
refactor: use abstract logger to reduce duplicate code (typeorm#9751)
Browse files Browse the repository at this point in the history
* feat(platform-tools): add more logging functions

* refactor(logger): add `AbstractLogger` to reduce duplicate code

---------

Co-authored-by: Christian Forgács <christian@wunderbit.de>
  • Loading branch information
christian-forgacs and Christian Forgács authored Feb 7, 2023
1 parent 9bd3a64 commit 12fdd73
Show file tree
Hide file tree
Showing 10 changed files with 698 additions and 502 deletions.
58 changes: 58 additions & 0 deletions docs/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,64 @@ export class MyCustomLogger implements Logger {
}
```

Or you can extend the `AbstractLogger` class:

```typescript
import { AbstractLogger } from "typeorm"

export class MyCustomLogger implements AbstractLogger {
/**
* Write log to specific output.
*/
protected writeLog(
level: LogLevel,
logMessage: LogMessage | LogMessage[],
queryRunner?: QueryRunner,
) {
const messages = this.prepareLogMessages(logMessage, {
highlightSql: false,
})

for (let message of messages) {
switch (message.type ?? level) {
case "log":
case "schema-build":
case "migration":
console.log(message.message)
break

case "info":
case "query":
if (message.prefix) {
console.info(message.prefix, message.message)
} else {
console.info(message.message)
}
break

case "warn":
case "query-slow":
if (message.prefix) {
console.warn(message.prefix, message.message)
} else {
console.warn(message.message)
}
break

case "error":
case "query-error":
if (message.prefix) {
console.error(message.prefix, message.message)
} else {
console.error(message.message)
}
break
}
}
}
}
```

And specify it in data source options:

```typescript
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export * from "./find-options/FindOptionsWhere"
export * from "./find-options/FindTreeOptions"
export * from "./find-options/JoinOptions"
export * from "./find-options/OrderByCondition"
export * from "./logger/AbstractLogger"
export * from "./logger/Logger"
export * from "./logger/LoggerOptions"
export * from "./logger/AdvancedConsoleLogger"
Expand Down
Loading

0 comments on commit 12fdd73

Please sign in to comment.