Skip to content

Commit a83b0e6

Browse files
feat(log): integrate @graphile/logger to allow log overrides (#109)
Co-authored-by: Sergey Trusov <[email protected]>
1 parent 5fe3db3 commit a83b0e6

File tree

16 files changed

+131
-52
lines changed

16 files changed

+131
-52
lines changed

__tests__/__snapshots__/settings.test.ts.snap

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ Object {
3030
"connectionString": "postgres://localhost:5432/dbname?ssl=true",
3131
"databaseName": "dbname",
3232
"databaseOwner": "dbname",
33+
"logger": Logger {
34+
"_logFactory": [Function],
35+
"_scope": Object {},
36+
"log": [Function],
37+
},
3338
"manageGraphileMigrateSchema": true,
3439
"migrationsFolder": "./migrations",
3540
"placeholders": undefined,
@@ -66,6 +71,11 @@ Object {
6671
"connectionString": "postgres://localhost:5432/dbname?ssl=true",
6772
"databaseName": "dbname",
6873
"databaseOwner": "dbname",
74+
"logger": Logger {
75+
"_logFactory": [Function],
76+
"_scope": Object {},
77+
"log": [Function],
78+
},
6979
"manageGraphileMigrateSchema": true,
7080
"migrationsFolder": "./migrations",
7181
"placeholders": undefined,
@@ -97,6 +107,11 @@ Object {
97107
"connectionString": "postgres://localhost:5432/dbname?ssl=true",
98108
"databaseName": "dbname",
99109
"databaseOwner": "dbname",
110+
"logger": Logger {
111+
"_logFactory": [Function],
112+
"_scope": Object {},
113+
"log": [Function],
114+
},
100115
"manageGraphileMigrateSchema": true,
101116
"migrationsFolder": "./migrations",
102117
"placeholders": undefined,
@@ -136,6 +151,11 @@ Object {
136151
"connectionString": "postgres://localhost:5432/dbname?ssl=true",
137152
"databaseName": "dbname",
138153
"databaseOwner": "dbname",
154+
"logger": Logger {
155+
"_logFactory": [Function],
156+
"_scope": Object {},
157+
"log": [Function],
158+
},
139159
"manageGraphileMigrateSchema": true,
140160
"migrationsFolder": "./migrations",
141161
"placeholders": undefined,
@@ -172,6 +192,11 @@ Object {
172192
"connectionString": "postgres://localhost:5432/dbname?ssl=true",
173193
"databaseName": "dbname",
174194
"databaseOwner": "dbname",
195+
"logger": Logger {
196+
"_logFactory": [Function],
197+
"_scope": Object {},
198+
"log": [Function],
199+
},
175200
"manageGraphileMigrateSchema": true,
176201
"migrationsFolder": "./migrations",
177202
"placeholders": undefined,

__tests__/settings.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ it("throws if shadow attempted but no shadow DB", async () => {
7777
`);
7878
});
7979

80+
it.each([[[]], [{}], ["test"]])(
81+
"throws error for invalid logger",
82+
async invalidLogger => {
83+
await expect(
84+
parseSettings({
85+
connectionString: exampleConnectionString,
86+
rootConnectionString: "notthesamestring1",
87+
shadowConnectionString: "notthesamestring2",
88+
logger: invalidLogger as any,
89+
}),
90+
).rejects.toMatchInlineSnapshot(`
91+
[Error: Errors occurred during settings validation:
92+
- Setting 'logger': Expected 'logger' to be a @graphile/logger Logger instance]
93+
`);
94+
},
95+
);
96+
8097
describe("makeRootDatabaseConnectionString", () => {
8198
it("modifies the database name", async () => {
8299
const parsedSettings = await parseSettings({

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
},
4040
"homepage": "https://github.com/graphile/migrate#readme",
4141
"dependencies": {
42+
"@graphile/logger": "^0.2.0",
4243
"@types/json5": "^0.0.30",
4344
"@types/node": "^14.6.0",
4445
"@types/pg": "^7.14.1",

src/actions.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Logger } from "@graphile/logger";
12
import { exec as rawExec } from "child_process";
23
import { promises as fsp } from "fs";
34
import { parse } from "pg-connection-string";
@@ -115,18 +116,16 @@ export async function executeActions(
115116
maxBuffer: 50 * 1024 * 1024,
116117
});
117118
if (stdout) {
118-
// eslint-disable-next-line no-console
119-
console.log(stdout);
119+
parsedSettings.logger.info(stdout);
120120
}
121121
if (stderr) {
122-
// eslint-disable-next-line no-console
123-
console.error(stderr);
122+
parsedSettings.logger.error(stderr);
124123
}
125124
}
126125
}
127126
}
128127

129-
export function makeValidateActionCallback(allowRoot = false) {
128+
export function makeValidateActionCallback(logger: Logger, allowRoot = false) {
130129
return async (inputValue: unknown): Promise<ActionSpec[]> => {
131130
const specs: ActionSpec[] = [];
132131
if (inputValue) {
@@ -141,8 +140,7 @@ export function makeValidateActionCallback(allowRoot = false) {
141140
!trueRawSpec["_"] &&
142141
typeof trueRawSpec["command"] === "string";
143142
if (isV003OrBelowCommand) {
144-
// eslint-disable-next-line no-console
145-
console.warn(
143+
logger.warn(
146144
"DEPRECATED: graphile-migrate now requires command action specs to have an `_: 'command'` property; we'll back-fill this for now, but please update your configuration",
147145
);
148146
}

src/commands/commit.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ export async function _commit(
100100
await fsp.writeFile(newMigrationFilepath, finalBody);
101101
await fsp.chmod(newMigrationFilepath, "440");
102102

103-
// eslint-disable-next-line no-console
104-
console.log(
103+
parsedSettings.logger.info(
105104
`graphile-migrate: New migration '${newMigrationFilename}' created`,
106105
);
107106
try {
@@ -113,13 +112,12 @@ export async function _commit(
113112
parsedSettings.blankMigrationContent.trim() + "\n",
114113
);
115114
} catch (e) {
116-
logDbError(e);
117-
// eslint-disable-next-line no-console
118-
console.error("ABORTING...");
115+
logDbError(parsedSettings, e);
116+
117+
parsedSettings.logger.error("ABORTING...");
119118
await writeCurrentMigration(parsedSettings, currentLocation, body);
120119
await fsp.unlink(newMigrationFilepath);
121-
// eslint-disable-next-line no-console
122-
console.error("ABORTED AND ROLLED BACK");
120+
parsedSettings.logger.error("ABORTED AND ROLLED BACK");
123121
throw e;
124122
}
125123
}

src/commands/migrate.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ export async function _migrate(
6464
parsedSettings.afterAllMigrations,
6565
);
6666
}
67-
// eslint-disable-next-line no-console
68-
console.log(
67+
parsedSettings.logger.info(
6968
`graphile-migrate${logSuffix}: ${
7069
remainingMigrations.length > 0
7170
? `${remainingMigrations.length} committed migrations executed`

src/commands/reset.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ export async function _reset(
3737
await pgClient.query(
3838
`DROP DATABASE IF EXISTS ${escapeIdentifier(databaseName)};`,
3939
);
40-
// eslint-disable-next-line no-console
41-
console.log(
40+
parsedSettings.logger.info(
4241
`graphile-migrate${logSuffix}: dropped database '${databaseName}'`,
4342
);
4443
try {
@@ -55,8 +54,7 @@ export async function _reset(
5554
await pgClient.query(
5655
`REVOKE ALL ON DATABASE ${escapeIdentifier(databaseName)} FROM PUBLIC;`,
5756
);
58-
// eslint-disable-next-line no-console
59-
console.log(
57+
parsedSettings.logger.info(
6058
`graphile-migrate${logSuffix}: recreated database '${databaseName}'`,
6159
);
6260
},

src/commands/uncommit.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ export async function _uncommit(parsedSettings: ParsedSettings): Promise<void> {
5151
await fsp.unlink(lastMigrationFilepath);
5252
await undoMigration(parsedSettings, lastMigration);
5353

54-
// eslint-disable-next-line no-console
55-
console.log(`graphile-migrate: migration '${lastMigrationFilepath}' undone`);
54+
parsedSettings.logger.info(
55+
`graphile-migrate: migration '${lastMigrationFilepath}' undone`,
56+
);
5657

5758
// Reset shadow
5859
await _reset(parsedSettings, true);

src/commands/watch.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ export function _makeCurrentMigrationRunner(
3232
let migrationsAreEquivalent = false;
3333

3434
try {
35-
// eslint-disable-next-line no-console
36-
console.log(`[${new Date().toISOString()}]: Running current.sql`);
35+
parsedSettings.logger.info(
36+
`[${new Date().toISOString()}]: Running current.sql`,
37+
);
3738
const start = process.hrtime();
3839
const connectionString = shadow
3940
? parsedSettings.shadowConnectionString
@@ -121,8 +122,7 @@ export function _makeCurrentMigrationRunner(
121122
);
122123
}
123124
} else {
124-
// eslint-disable-next-line no-console
125-
console.log(
125+
parsedSettings.logger.info(
126126
`[${new Date().toISOString()}]: current.sql unchanged, skipping migration`,
127127
);
128128
}
@@ -154,16 +154,15 @@ export function _makeCurrentMigrationRunner(
154154
}
155155
const interval2 = process.hrtime(start);
156156
const duration2 = interval2[0] * 1e3 + interval2[1] * 1e-6;
157-
// eslint-disable-next-line no-console
158-
console.log(
157+
parsedSettings.logger.info(
159158
`[${new Date().toISOString()}]: Finished (${duration2.toFixed(0)}ms${
160159
duration2 - duration >= 5
161160
? `; excluding actions: ${duration.toFixed(0)}ms`
162161
: ""
163162
})`,
164163
);
165164
} catch (e) {
166-
logDbError(e);
165+
logDbError(parsedSettings, e);
167166
throw e;
168167
}
169168
}
@@ -199,10 +198,12 @@ export async function _watch(
199198
running = true;
200199

201200
run()
202-
.catch(e => {
203-
if (!e["_gmlogged"]) {
204-
// eslint-disable-next-line no-console
205-
console.error(e);
201+
.catch(error => {
202+
if (!error["_gmlogged"]) {
203+
parsedSettings.logger.error(
204+
`Error occurred whilst processing migration: ${error.message}`,
205+
{ error },
206+
);
206207
}
207208
})
208209
.finally(() => {

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
export * from "./commands";
2+
export { Settings } from "./settings";
3+
export { Logger, LogFunctionFactory, LogLevel } from "@graphile/logger";
4+
export { defaultLogger } from "./logger";

0 commit comments

Comments
 (0)