Logging

Enabling logging

You can enable logging of all queries and errors by simply setting logging: true in your connection options:

  1. {
  2. name: "mysql",
  3. type: "mysql",
  4. host: "localhost",
  5. port: 3306,
  6. username: "test",
  7. password: "test",
  8. database: "test",
  9. ...
  10. logging: true
  11. }

Logging options

You can enable different types of logging in connection options:

  1. {
  2. host: "localhost",
  3. ...
  4. logging: ["query", "error"]
  5. }

If you want to enable logging of failed queries only then only add error:

  1. {
  2. host: "localhost",
  3. ...
  4. logging: ["error"]
  5. }

There are other options you can use:

  • query - logs all queries.
  • error - logs all failed queries and errors.
  • schema - logs the schema build process.
  • warn - logs internal orm warnings.
  • info - logs internal orm informative messages.
  • log - logs 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":

  1. {
  2. host: "localhost",
  3. ...
  4. logging: "all"
  5. }

Log long-running queries

If you have performance issues, you can log queries that take too much time to executeby setting maxQueryExecutionTime in connection options:

  1. {
  2. host: "localhost",
  3. ...
  4. maxQueryExecutionTime: 1000
  5. }

This code will log all queries which run more then 1 second.

Changing default logger

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 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 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:

  1. {
  2. host: "localhost",
  3. ...
  4. logging: true,
  5. logger: "file"
  6. }

Using custom logger

You can create your own logger class by implementing the Logger interface:

  1. import {Logger} from "typeorm";
  2. export class MyCustomLogger implements Logger {
  3. // implement all methods from logger class
  4. }

And specify it in connection options:

  1. import {createConnection} from "typeorm";
  2. import {MyCustomLogger} from "./logger/MyCustomLogger";
  3. createConnection({
  4. name: "mysql",
  5. type: "mysql",
  6. host: "localhost",
  7. port: 3306,
  8. username: "test",
  9. password: "test",
  10. database: "test",
  11. logger: new MyCustomLogger()
  12. });

If you defined your connection options in the ormconfig file,then you can use it and override it in the following way:

  1. import {createConnection, getConnectionOptions} from "typeorm";
  2. import {MyCustomLogger} from "./logger/MyCustomLogger";
  3. // getConnectionOptions will read options from your ormconfig file
  4. // and return it in connectionOptions object
  5. // then you can simply append additional properties to it
  6. getConnectionOptions().then(connectionOptions => {
  7. return createConnection(Object.assign(connectionOptions, {
  8. logger: new MyCustomLogger()
  9. }))
  10. });

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:

  1. // user sends request during entity save
  2. postRepository.save(post, { data: { request: request } });
  3. // in logger you can access it this way:
  4. logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner) {
  5. const requestUrl = queryRunner && queryRunner.data["request"] ? "(" + queryRunner.data["request"].url + ") " : "";
  6. console.log(requestUrl + "executing query: " + sql);
  7. }