simple-console - this is a simple console logger which is exactly the same as the advanced logger, but it does not use any color highlighting.
This logger can be used if you have problems / or don't like colorized logs.
file - this logger writes all logs into ormlogs.log in the root folder of your project (near package.json and ormconfig.json).
debug - this logger uses debug package, to turn on logging set your env variable DEBUG=typeorm:* (note logging option has no effect on this logger).
You can enable any of them in connection options:
Using custom logger
You can create your own logger class by implementing the Logger interface:
And specify it in connection options:
If you defined your connection options in the ormconfig file, then you can use it and override it in the following way:
Logger methods can accept QueryRunner when it's available. It's helpful if you want to log additional data. Also, via query runner, you can get access to additional data passed during persist/remove. For example:
import {Logger} from "typeorm";
export class MyCustomLogger implements Logger {
// implement all methods from logger class
}
import {createConnection} from "typeorm";
import {MyCustomLogger} from "./logger/MyCustomLogger";
createConnection({
name: "mysql",
type: "mysql",
host: "localhost",
port: 3306,
username: "test",
password: "test",
database: "test",
logger: new MyCustomLogger()
});
import {createConnection, getConnectionOptions} from "typeorm";
import {MyCustomLogger} from "./logger/MyCustomLogger";
// getConnectionOptions will read options from your ormconfig file
// and return it in connectionOptions object
// then you can simply append additional properties to it
getConnectionOptions().then(connectionOptions => {
return createConnection(Object.assign(connectionOptions, {
logger: new MyCustomLogger()
}))
});
// user sends request during entity save
postRepository.save(post, { data: { request: request } });
// in logger you can access it this way:
logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner) {
const requestUrl = queryRunner && queryRunner.data["request"] ? "(" + queryRunner.data["request"].url + ") " : "";
console.log(requestUrl + "executing query: " + query);
}