Skip to content

Commit

Permalink
feat: FileLogger accepts custom file path (#6642)
Browse files Browse the repository at this point in the history
This allows users to override default log filepath and save typeorm logs in a custom location

Closes: #4410
  • Loading branch information
artysidorenko committed Sep 2, 2020
1 parent 85c07fd commit c99ba40
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/logger/FileLogger.ts
@@ -1,4 +1,4 @@
import {LoggerOptions} from "./LoggerOptions";
import {LoggerOptions, FileLoggerOptions} from "./LoggerOptions";
import {QueryRunner} from "../query-runner/QueryRunner";
import {Logger} from "./Logger";
import {PlatformTools} from "../platform/PlatformTools";
Expand All @@ -13,7 +13,10 @@ export class FileLogger implements Logger {
// Constructor
// -------------------------------------------------------------------------

constructor(private options?: LoggerOptions) {
constructor(
private options?: LoggerOptions,
private fileLoggerOptions?: FileLoggerOptions,
) {
}

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -97,9 +100,13 @@ export class FileLogger implements Logger {
*/
protected write(strings: string|string[]) {
strings = Array.isArray(strings) ? strings : [strings];
const basePath = PlatformTools.load("app-root-path").path;
const basePath = PlatformTools.load("app-root-path").path + "/";
let logPath = "ormlogs.log";
if (this.fileLoggerOptions && this.fileLoggerOptions.logPath) {
logPath = PlatformTools.pathNormalize(this.fileLoggerOptions.logPath);
}
strings = (strings as string[]).map(str => "[" + new Date().toISOString() + "]" + str);
PlatformTools.appendFileSync(basePath + "/ormlogs.log", strings.join("\r\n") + "\r\n"); // todo: use async or implement promises?
PlatformTools.appendFileSync(basePath + logPath, strings.join("\r\n") + "\r\n"); // todo: use async or implement promises?
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/logger/LoggerOptions.ts
Expand Up @@ -2,3 +2,13 @@
* Logging options.
*/
export type LoggerOptions = boolean|"all"|("query"|"schema"|"error"|"warn"|"info"|"log"|"migration")[];

/**
* File logging option.
*/
export type FileLoggerOptions = {
/**
* Specify custom path for log file, relative to application root
*/
logPath: string;
};
12 changes: 12 additions & 0 deletions test/github-issues/4410/entity/Username.ts
@@ -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;
}
81 changes: 81 additions & 0 deletions test/github-issues/4410/issue-4410.ts
@@ -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)
);
})));
});
});

0 comments on commit c99ba40

Please sign in to comment.