Logging
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
- logs all queries.schema
- logs the schema build process.warn
- logs internal orm warnings.info
- logs internal orm informative messages.log
- logs internal orm log messages.
If you have performance issues, you can log queries that take too much time to executeby setting maxQueryExecutionTime
in connection options:
{
host: "localhost",
maxQueryExecutionTime: 1000
}
This code will log all queries which run more then 1 second
.
TypeORM ships with 4 different types of logger:
advanced-console
- this is the default logger which logs all messages into the console using colorand sql syntax highlighting (using ).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 intoormlogs.log
in the root folder of your project (nearpackage.json
andormconfig.json
).debug
- this logger uses debug package, to turn on logging set your env variableDEBUG=typeorm:*
(note logging option has no effect on this logger).
{
host: "localhost",
...
logging: true,
logger: "file"
}
You can create your own logger class by implementing the Logger
interface:
And specify it in connection options:
import {createConnection} from "typeorm";
import {MyCustomLogger} from "./logger/MyCustomLogger";
name: "mysql",
type: "mysql",
host: "localhost",
port: 3306,
username: "test",
password: "test",
database: "test",
logger: new MyCustomLogger()
});
If you defined your connection options in the ormconfig
file,then you can use it and override it in the 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()
}))
});