Skip to content

Commit 292fc1d

Browse files
committed
add logging
1 parent 10b9642 commit 292fc1d

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ main();
4343
* [Streaming result rows](https://bitbucket.org/adp-developers/snowflake-promise/src/master/examples/streaming.js)
4444
* [Using traditional Promise `then` syntax (and older versions of Node.js)](https://bitbucket.org/adp-developers/snowflake-promise/src/master/examples/oldSchool.js)
4545
* Node 4.0.0 is the oldest supported version
46+
* [Turn on logging](https://bitbucket.org/adp-developers/snowflake-promise/src/master/examples/logging.js)

examples/logging.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Two types of logging are available.
3+
//
4+
// * SDK logging messages are emitted by the Snowflake SDK. These can be turned on by
5+
// setting logLevel to the desired level (such as 'trace').
6+
//
7+
// * You can log SQL queries. This is enabled by passing a _function_ that will receive
8+
// a string to be logged. The string includes the database and schema name, the sqlText,
9+
// and the (local) elapsed time.
10+
//
11+
12+
const Snowflake = require('snowflake-promise').Snowflake;
13+
14+
async function main() {
15+
const snowflake = new Snowflake({
16+
account: '<account name>',
17+
username: '<username>',
18+
password: '<password>',
19+
database: 'SNOWFLAKE_SAMPLE_DATA',
20+
schema: 'TPCH_SF1',
21+
warehouse: 'DEMO_WH'
22+
}, {
23+
logLevel: 'trace', // maximum SDK logLevel
24+
logSql: console.log // SQL statements will be logged to the console
25+
});
26+
27+
await snowflake.connect();
28+
29+
const rows = await snowflake.execute(
30+
'SELECT COUNT(*) FROM CUSTOMER WHERE C_MKTSEGMENT=:1',
31+
['AUTOMOBILE']
32+
);
33+
34+
console.log(rows);
35+
}
36+
37+
main();

src/Snowflake.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import * as SDK from 'snowflake-sdk';
22

33
import { ConnectionOptions } from './types/ConnectionOptions';
44
import { ExecuteOptions } from './types/ExecuteOptions';
5+
import { LoggingOptions } from './types/LoggingOptions';
56
import { Statement } from './Statement';
67

78
export class Snowflake {
89
private readonly sdk_connection;
10+
private readonly logSql: (sqlText: string) => void;
911

1012
/* Creates a new Snowflake instance. */
11-
constructor(private readonly connectionOptions: ConnectionOptions) {
13+
constructor(connectionOptions: ConnectionOptions, loggingOptions: LoggingOptions​​ = {}) {
14+
if (loggingOptions.logLevel) { SDK.configure({ logLevel: loggingOptions.logLevel }); }
15+
this.logSql = loggingOptions.logSql || null;
1216
this.sdk_connection = SDK.createConnection(connectionOptions);
1317
}
1418

@@ -40,7 +44,7 @@ export class Snowflake {
4044

4145
/** Create a Statement. */
4246
createStatement(options: ExecuteOptions) {
43-
return new Statement(this.sdk_connection, options);
47+
return new Statement(this.sdk_connection, options, this.logSql);
4448
}
4549

4650
/** A convenience function to execute a SQL statement and return the resulting rows. */

src/Statement.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export class Statement {
1717
*/
1818
constructor(
1919
private readonly connection: any,
20-
private readonly executeOptions: ExecuteOptions
20+
private readonly executeOptions: ExecuteOptions,
21+
private readonly logSql: (sqlText: string) => void = null
2122
) {}
2223

2324
/**
@@ -29,12 +30,17 @@ export class Statement {
2930
if (this.executePromise) { throw new StatementAlreadyExecutedError(); }
3031

3132
this.executePromise = new Promise((resolve, reject) => {
33+
let startTime: number;
34+
3235
this.executeOptions['complete'] = (err, stmt, rows) => {
36+
const elapsed = Date.now() - startTime;
3337
if (err) { reject(err); }
38+
if (this.logSql) { this.log(elapsed); }
3439
this.rows = rows;
3540
resolve();
3641
};
3742

43+
startTime = Date.now();
3844
this.stmt = this.connection.execute(this.executeOptions);
3945
});
4046

@@ -135,4 +141,17 @@ export class Statement {
135141
if (!this.executePromise) { throw new StatementNotExecutedError(); }
136142
return this.stmt.getStatementtId();
137143
}
144+
145+
/** log execution details */
146+
private log(elapsedTime: number) {
147+
const state = this.getSessionState();
148+
const db = state.getCurrentDatabase();
149+
const schema = state.getCurrentSchema();
150+
const sqlText = this.getSqlText();
151+
152+
let logMessage = `Executed (${db}.${schema}): ${sqlText}`;
153+
if (logMessage[logMessage.length - 1] !== ';') { logMessage += ';'; }
154+
logMessage += ` Elapsed time: ${elapsedTime}ms`;
155+
this.logSql(logMessage);
156+
}
138157
}

src/types/LoggingOptions.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface LoggingOptions {
2+
/** optional function to log SQL statements (e.g. console.log) */
3+
logSql?: (sqlText: string) => void;
4+
/** turn on SDK-level logging */
5+
logLevel?: 'error' | 'warn' | 'debug' | 'info' | 'trace';
6+
}

0 commit comments

Comments
 (0)