forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: FileLogger accepts custom file path
This allows users to override default log filepath and save typeorm logs in a custom location Closes: typeorm#4410
- Loading branch information
1 parent
d1ed572
commit 0e70a60
Showing
4 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Column } from "../../../../src/decorator/columns/Column"; | ||
import { PrimaryColumn } from "../../../../src/decorator/columns/PrimaryColumn"; | ||
import { Entity } from "../../../../src/decorator/entity/Entity"; | ||
|
||
@Entity() | ||
export class Username { | ||
@PrimaryColumn() | ||
username: string; | ||
|
||
@Column() | ||
email: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import sinon from "sinon"; | ||
import { Connection, FileLogger } from "../../../src"; | ||
import { createTestingConnections, reloadTestingDatabases, closeTestingConnections, TestingOptions } from "../../utils/test-utils"; | ||
import { Username } from "./entity/Username"; | ||
import { PlatformTools } from "../../../src/platform/PlatformTools"; | ||
|
||
describe("github issues > #4410 allow custom filepath for FileLogger", () => { | ||
let connections: Connection[]; | ||
let stub: sinon.SinonStub; | ||
|
||
const testingOptions: TestingOptions = { | ||
entities: [Username], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
}; | ||
const testQuery = "SELECT COUNT(*) from username;"; | ||
|
||
before(() => stub = sinon.stub(PlatformTools, "appendFileSync")); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
afterEach(async () => { | ||
stub.resetHistory(); await closeTestingConnections(connections); | ||
}); | ||
|
||
describe("when no option is passed", () => { | ||
before(async () => { | ||
connections = await createTestingConnections({ | ||
...testingOptions, | ||
createLogger: () => new FileLogger("all"), | ||
}); | ||
}); | ||
it("writes to the base path", async () => | ||
Promise.all(connections.map(async (connection) => { | ||
await connection.query(testQuery); | ||
sinon.assert.calledWith( | ||
stub, | ||
PlatformTools.load("app-root-path").path + "/ormlogs.log", | ||
sinon.match(testQuery) | ||
); | ||
}))); | ||
}); | ||
|
||
describe("when logPath option is passed as a file", () => { | ||
before(async () => { | ||
connections = await createTestingConnections({ | ||
...testingOptions, | ||
createLogger: () => new FileLogger("all", { | ||
logPath: "test.log" | ||
}), | ||
}); | ||
}); | ||
it("writes to the given filename", async () => | ||
Promise.all(connections.map(async (connection) => { | ||
await connection.query(testQuery); | ||
sinon.assert.calledWith( | ||
stub, | ||
PlatformTools.load("app-root-path").path + "/test.log", | ||
sinon.match(testQuery) | ||
); | ||
}))); | ||
}); | ||
|
||
describe("when logPath option is passed as a nested path", () => { | ||
before(async () => { | ||
connections = await createTestingConnections({ | ||
...testingOptions, | ||
createLogger: () => new FileLogger("all", { | ||
logPath: "./test/test.log" | ||
}), | ||
}); | ||
}); | ||
it("writes to the given path", () => | ||
Promise.all(connections.map(async (connection) => { | ||
await connection.query(testQuery); | ||
sinon.assert.calledWith( | ||
stub, | ||
PlatformTools.load("app-root-path").path + "/test/test.log", | ||
sinon.match(testQuery) | ||
); | ||
}))); | ||
}); | ||
}); |