Skip to content

Commit

Permalink
added different logging strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
Umed Khudoiberdiev committed Jul 27, 2017
1 parent 3ecd594 commit b2be4b4
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 70 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.vscode/
.idea/
build/
coverage/
node_modules/
npm-debug.log
ormconfig.json
.vscode
.idea/
ormlogs.log
npm-debug.log
6 changes: 3 additions & 3 deletions docs/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ You can enable all queries logging by simply setting `logging: true` in your con
}
```

This configuration will enable all executing queries logging + errors of failed queries.
This configuration will enable all executing queries logging and failed query errors.

## Logging options

Expand Down Expand Up @@ -99,8 +99,8 @@ You can enable any of them in connection options this way:
{
host: "localhost",
...
logger: "simple-console",
logging: true
logging: true,
logger: "file"
}
```

Expand Down
28 changes: 7 additions & 21 deletions ormconfig.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
"username": "test",
"password": "test",
"database": "test",
"logging": {
"logQueries": false
}
"logging": false
},
{
"skip": false,
Expand All @@ -21,18 +19,14 @@
"username": "test",
"password": "test",
"database": "test",
"logging": {
"logQueries": false
}
"logging": false
},
{
"skip": false,
"name": "sqlite",
"type": "sqlite",
"database": "temp/sqlitedb.db",
"logging": {
"logQueries": false
}
"logging": false
},
{
"skip": false,
Expand All @@ -43,9 +37,7 @@
"username": "test",
"password": "test",
"database": "test",
"logging": {
"logQueries": false
}
"logging": false
},
{
"skip": true,
Expand All @@ -55,9 +47,7 @@
"username": "sa",
"password": "admin",
"database": "test",
"logging": {
"logQueries": false
}
"logging": false
},
{
"skip": true,
Expand All @@ -68,18 +58,14 @@
"password": "oracle",
"port": 1521,
"sid": "xe.oracle.docker",
"logging": {
"logQueries": false
}
"logging": false
},
{
"skip": false,
"disabledIfNotEnabledImplicitly": true,
"name": "mongodb",
"type": "mongodb",
"database": "test",
"logging": {
"logQueries": false
}
"logging": false
}
]
66 changes: 39 additions & 27 deletions sample/sample1-simple-entity/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "reflect-metadata";
import {ConnectionOptions, createConnection} from "../../src/index";
import {Post} from "./entity/Post";
import {QueryFailedError} from "../../src/error/QueryFailedError";

const options: ConnectionOptions = {
// type: "oracle",
Expand All @@ -10,13 +9,13 @@ const options: ConnectionOptions = {
// password: "oracle",
// port: 1521,
// sid: "xe.oracle.docker",
// "name": "mysql",
// "type": "mysql",
// "host": "localhost",
// "port": 3306,
// "username": "test",
// "password": "test",
// "database": "test",
"name": "mysql",
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "test",
"password": "test",
"database": "test",
// type: "postgres",
// host: "localhost",
// port: 5432,
Expand All @@ -29,35 +28,48 @@ const options: ConnectionOptions = {
// "password": "admin12345",
// "database": "test",
// port: 1521,
type: "sqlite",
database: "temp/sqlitedb.db",
logging: ["query", "error"],
// type: "sqlite",
// database: "temp/sqlitedb.db",
// logger: "file",
// logging: ["query", "error"],
// logging: ["error", "schema", "query"],
// maxQueryExecutionTime: 90,
autoSchemaSync: true,
entities: [Post]
};

createConnection(options).then(async connection => {

try {
await connection.query("CREATE DATABASE 'aaaa' AND DIE");
// const posts: Post[] = [];
// for (let i = 0; i < 100; i++) {
// let post = new Post();
// post.text = "Hello how are you?";
// post.title = "hello";
// post.likesCount = 100;
// posts.push(post);
// }
//
// await connection.manager.save(posts);

} catch (err) {
console.log("-------------------------------");
console.log("ERRROR: ", err instanceof QueryFailedError);
console.log(err);
}
// try {
// await connection.query("CREATE DATABASE 'aaaa' AND DIE");
//
// } catch (err) {
// console.log("-------------------------------");
// console.log("ERRROR: ", err instanceof QueryFailedError);
// console.log(err);
// }

// let post = new Post();
// post.text = "Hello how are you?";
// post.title = "hello";
// post.likesCount = 100;
let post = new Post();
post.text = "Hello how are you?";
post.title = "hello";
post.likesCount = 100;

// let postRepository = connection.getRepository(Post);
let postRepository = connection.getRepository(Post);

// postRepository
// .save(post)
// .then(post => console.log("Post has been saved: ", post))
// .catch(error => console.log("Cannot save. Error: ", error));
postRepository
.save(post)
.then(post => console.log("Post has been saved: ", post))
.catch(error => console.log("Cannot save. Error: ", error));

}, error => console.log("Cannot connect: ", error));
9 changes: 9 additions & 0 deletions src/driver/mysql/MysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,16 @@ export class MysqlQueryRunner implements QueryRunner {
return new Promise(async (ok, fail) => {
const databaseConnection = await this.connect();
this.driver.connection.logger.logQuery(query, parameters, this);
const queryStartTime = +new Date();
databaseConnection.query(query, parameters, (err: any, result: any) => {

// log slow queries if maxQueryExecution time is set
const maxQueryExecutionTime = this.driver.connection.options.maxQueryExecutionTime;
const queryEndTime = +new Date();
const queryExecutionTime = queryEndTime - queryStartTime;
if (maxQueryExecutionTime && queryExecutionTime > maxQueryExecutionTime)
this.driver.connection.logger.logQuerySlow(queryExecutionTime, query, parameters, this);

if (err) {
this.driver.connection.logger.logQueryError(err, query, parameters, this);
return fail(new QueryFailedError(query, parameters, err));
Expand Down
10 changes: 10 additions & 0 deletions src/driver/oracle/OracleQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,17 @@ export class OracleQueryRunner implements QueryRunner {

return new Promise(async (ok, fail) => {
this.driver.connection.logger.logQuery(query, parameters, this);
const queryStartTime = +new Date();

const handler = (err: any, result: any) => {

// log slow queries if maxQueryExecution time is set
const maxQueryExecutionTime = this.driver.connection.options.maxQueryExecutionTime;
const queryEndTime = +new Date();
const queryExecutionTime = queryEndTime - queryStartTime;
if (maxQueryExecutionTime && queryExecutionTime > maxQueryExecutionTime)
this.driver.connection.logger.logQuerySlow(queryExecutionTime, query, parameters, this);

if (err) {
this.driver.connection.logger.logQueryError(err, query, parameters, this);
return fail(new QueryFailedError(query, parameters, err));
Expand Down
10 changes: 10 additions & 0 deletions src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,17 @@ export class PostgresQueryRunner implements QueryRunner {
return new Promise<any[]>(async (ok, fail) => {
const databaseConnection = await this.connect();
this.driver.connection.logger.logQuery(query, parameters, this);
const queryStartTime = +new Date();

databaseConnection.query(query, parameters, (err: any, result: any) => {

// log slow queries if maxQueryExecution time is set
const maxQueryExecutionTime = this.driver.connection.options.maxQueryExecutionTime;
const queryEndTime = +new Date();
const queryExecutionTime = queryEndTime - queryStartTime;
if (maxQueryExecutionTime && queryExecutionTime > maxQueryExecutionTime)
this.driver.connection.logger.logQuerySlow(queryExecutionTime, query, parameters, this);

if (err) {
this.driver.connection.logger.logQueryError(err, query, parameters, this);
fail(new QueryFailedError(query, parameters, err));
Expand Down
9 changes: 9 additions & 0 deletions src/driver/sqlite/SqliteQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,16 @@ export class SqliteQueryRunner implements QueryRunner {
return new Promise<any[]>(async (ok, fail) => {
const databaseConnection = await this.connect();
this.driver.connection.logger.logQuery(query, parameters, this);
const queryStartTime = +new Date();
databaseConnection.all(query, parameters, (err: any, result: any) => {

// log slow queries if maxQueryExecution time is set
const maxQueryExecutionTime = this.driver.connection.options.maxQueryExecutionTime;
const queryEndTime = +new Date();
const queryExecutionTime = queryEndTime - queryStartTime;
if (maxQueryExecutionTime && queryExecutionTime > maxQueryExecutionTime)
this.driver.connection.logger.logQuerySlow(queryExecutionTime, query, parameters, this);

if (err) {
this.driver.connection.logger.logQueryError(err, query, parameters, this);
fail(new QueryFailedError(query, parameters, err));
Expand Down
8 changes: 8 additions & 0 deletions src/driver/sqlserver/SqlServerQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,16 @@ export class SqlServerQueryRunner implements QueryRunner {
}
});
}
const queryStartTime = +new Date();
request.query(query, (err: any, result: any) => {

// log slow queries if maxQueryExecution time is set
const maxQueryExecutionTime = this.driver.connection.options.maxQueryExecutionTime;
const queryEndTime = +new Date();
const queryExecutionTime = queryEndTime - queryStartTime;
if (maxQueryExecutionTime && queryExecutionTime > maxQueryExecutionTime)
this.driver.connection.logger.logQuerySlow(queryExecutionTime, query, parameters, this);

const resolveChain = () => {
if (promiseIndex !== -1)
this.queryResponsibilityChain.splice(promiseIndex, 1);
Expand Down
13 changes: 12 additions & 1 deletion src/driver/websql/WebsqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,22 @@ export class WebsqlQueryRunner implements QueryRunner {

return new Promise(async (ok, fail) => {

this.driver.connection.logger.logQuery(query, parameters, this);
const db = await this.connect();
// todo(dima): check if transaction is not active

this.driver.connection.logger.logQuery(query, parameters, this);
const queryStartTime = +new Date();

db.transaction((tx: any) => {
tx.executeSql(query, parameters, (tx: any, result: any) => {

// log slow queries if maxQueryExecution time is set
const maxQueryExecutionTime = this.driver.connection.options.maxQueryExecutionTime;
const queryEndTime = +new Date();
const queryExecutionTime = queryEndTime - queryStartTime;
if (maxQueryExecutionTime && queryExecutionTime > maxQueryExecutionTime)
this.driver.connection.logger.logQuerySlow(queryExecutionTime, query, parameters, this);

const rows = Object
.keys(result.rows)
.filter(key => key !== "length")
Expand Down
2 changes: 1 addition & 1 deletion src/logger/AdvancedConsoleLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const chalk = require("chalk");

/**
* Performs logging of the events in TypeORM.
* This version of logger uses console to log events and does not use syntax highlighting.
* This version of logger uses console to log events and use syntax highlighting.
*/
export class AdvancedConsoleLogger implements Logger {

Expand Down
Loading

0 comments on commit b2be4b4

Please sign in to comment.