4.2 KiB
Logging
- Enabling logging
- Logging options
- Log long-running queries
- Changing default logger
- Using custom logger
Enabling logging
You can enable logging of all queries and errors by simply setting logging: true in your connection options:
{
name: "mysql",
type: "mysql",
host: "localhost",
port: 3306,
username: "test",
password: "test",
database: "test",
...
logging: true
}
Logging options
You can enable different types of logging in connection options:
{
host: "localhost",
...
logging: ["query", "error"]
}
If you want to enable logging of failed queries only then only add error:
{
host: "localhost",
...
logging: ["error"]
}
There are other options you can use:
query- log all querieserror- log all failed queries and errorschema- log the schema build processwarn- log internal orm warningsinfo- log internal orm informative messageslog- log internal orm log messages
You can specify as many options as needed.
If you want to enable all logging you can simply specify logging: "all":
{
host: "localhost",
...
logging: "all"
}
Log long-running queries
If you have performance issues you can log queries that take too much time to execute
by setting maxQueryExecutionTime in connection options:
{
host: "localhost",
...
maxQueryExecutionTime: 1000
}
This code will log all queries which run more then 1 second.
Changing default logger
TypeORM ships with 3 different types of logger:
advanced-console- this is default which logs all messages onto the console using color and sql syntax highlighting (using chalk)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 logsfile- this logger writes all logs intoormlogs.login the root folder of your project (nearpackage.jsonandormconfig.json)
You can enable any of them in connection options:
{
host: "localhost",
...
logging: true,
logger: "file"
}
Using custom logger
You can create your own logger class by implementing the Logger interface:
import {Logger} from "typeorm";
export class MyCustomLogger implements Logger {
// implement all methods from logger class
}
And specify it in connection options:
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()
});
If you defined your connection options in ormconfig file,
then you can use it and override following way:
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()
}))
});
Logger methods can accept QueryRunner when its available. Its 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:
// 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: " + sql);
}