typeorm-logger-adaptor
v1.3.1
Published
Logger adaptors for TypeORM
Maintainers
Readme
typeorm-logger-adaptor
Logger adaptors for TypeORM.
Supported versions
- TypeORM 0.2.x, 0.3.x and 1.x
- Note: TypeORM 1.x requires Node.js 20.19 or later.
- Bunyan 1.8.5 or later
- Winston 3.0.0 or later
Install
# NPM
npm install typeorm-logger-adaptor
# Yarn
yarn add typeorm-logger-adaptorHow to use
- Install
typeorm-logger-adaptor,typeormand a logger library you want to use. import { XxxAdaptor } from 'typeorm-logger-adaptor/xxx';.- Replace
xxxwith logger library name (e.g.winston,bunyan).
- Replace
- Create and configure a logger instance.
- Create an adaptor instance and configure TypeORM connection.
- See here for details.
Example
import {DataSource} from 'typeorm';
import * as winston from 'winston';
import {WinstonAdaptor} from 'typeorm-logger-adaptor/winston';
async function example(): Promise<void> {
// Configure logger (Winston)
const logger = winston.createLogger({
level: 'debug',
format: winston.format.cli(),
transports: [new winston.transports.Console()],
});
// Create and initialize DataSource
const dataSource = await new DataSource({
type: 'mysql',
host: 'database-host',
port: 3306,
username: 'conn_user',
password: 'conn_user_pw',
database: 'database_name',
// Use logger adaptor like this
logger: new WinstonAdaptor(logger, 'all', {highlightSql: true, formatSql: true}),
}).initialize();
try {
await dataSource.synchronize();
await dataSource.runMigrations();
await dataSource.query('SELECT count(1) FROM user WHERE name = ?', ['Taro']);
await dataSource.query('SELECT ___column_that_does_not_exist___ FROM ___table_that_does_not_exist___');
} finally {
await dataSource.destroy();
}
}Adaptor options
The third constructor argument accepts an options object.
new WinstonAdaptor(logger, 'all', {
highlightSql: true, // Enables SQL syntax highlighting.
formatSql: true, // Enables SQL pretty-printing.
loggerMethodMapping: {...}, // Maps Winston logger methods to TypeORM logger methods.
});
new BunyanAdaptor(logger, 'all', {
// Note: highlightSql is not supported here because ANSI escape sequences would pollute Bunyan's JSON logs.
formatSql: true, // Enables SQL pretty-printing.
logLevelMapping: {...}, // Specifies Bunyan log levels that each TypeORM logger method uses.
});License
MIT License.
Copyright (c) 2020 KOMIYA Atsushi.
