Skip to content

Commit

Permalink
fix: create log dir in serverless environment (#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Mar 22, 2021
1 parent ae9ed26 commit 8a15f69
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages-serverless/express-layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const fs = require('fs');
const request = require('request');

const socketPath = join(os.tmpdir(), `server-${Date.now()}.sock`);
const logDir = join(os.tmpdir(), `app-${Date.now()}`);

process.env.MIDWAY_LOGGER_WRITEABLE_DIR = logDir;

module.exports = engine => {
engine.addRuntimeExtension({
Expand Down
3 changes: 3 additions & 0 deletions packages-serverless/koa-layer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const fs = require('fs');
const request = require('request');

const socketPath = join(os.tmpdir(), `server-${Date.now()}.sock`);
const logDir = join(os.tmpdir(), `app-${Date.now()}`);

process.env.MIDWAY_LOGGER_WRITEABLE_DIR = logDir;

module.exports = engine => {
engine.addRuntimeExtension({
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,5 @@ export interface IMidwayFramework<APP extends IMidwayApplication, T extends ICon
getProjectName(): string;
getDefaultContextLoggerClass(): any;
}

export const MIDWAY_LOGGER_WRITEABLE_DIR = 'MIDWAY_LOGGER_WRITEABLE_DIR';
9 changes: 8 additions & 1 deletion packages/core/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createLogger, LoggerOptions } from '@midwayjs/logger';
import { IMidwayFramework } from './interface';
import { IMidwayFramework, MIDWAY_LOGGER_WRITEABLE_DIR } from './interface';
import { isDevelopmentEnvironment, getUserHome } from './util';
import { join } from 'path';

Expand All @@ -17,5 +17,12 @@ export const createMidwayLogger = (
: join(getUserHome(), 'logs', framework.getProjectName()),
level: isDevelopmentEnv ? 'info' : 'warn',
};
if (process.env[MIDWAY_LOGGER_WRITEABLE_DIR]) {
loggerOptions.dir = join(
process.env[MIDWAY_LOGGER_WRITEABLE_DIR],
'logs',
framework.getProjectName()
);
}
return createLogger(name, Object.assign({}, loggerOptions, options));
};
2 changes: 1 addition & 1 deletion packages/core/test/baseFramework.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import {
IMidwayApplication,
IMidwayBootstrapOptions,
MidwayRequestContainer,
LightFramework,
} from '../src';
import * as mm from 'mm';
import { LifeCycleTest, LifeCycleTest1, TestBinding } from './fixtures/lifecycle';
import { LightFramework } from '../src/util/emptyFramework';
import sinon = require('sinon');

@Provide()
Expand Down
24 changes: 23 additions & 1 deletion packages/core/test/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createMidwayLogger, EmptyFramework } from '../src';
import { createMidwayLogger, EmptyFramework, LightFramework } from '../src';
import { MidwayContextLogger } from '@midwayjs/logger';
import { join } from 'path';
import * as mm from 'mm';
import { tmpdir } from 'os';
import { existsSync } from 'fs';

describe('/test/logger.test.ts', () => {
it('should create context logger', async () => {
Expand All @@ -25,4 +28,23 @@ describe('/test/logger.test.ts', () => {
contextLogger.log('hello world');
contextLogger.log('info', 'hello world');
});

it('should create log in serverless environment', async () => {
const tmpLogsDir = join(tmpdir(), Date.now() + '-' + (Math.random() * 100000).toString().slice(-5));
mm(process.env, 'MIDWAY_LOGGER_WRITEABLE_DIR', tmpLogsDir);
const framework = new LightFramework();
await framework.initialize({
baseDir: join(
__dirname,
'./fixtures/base-app/src'
),
});
const logger = framework.getCoreLogger();
logger.info('hello world');
logger.debug('hello world');
logger.warn('hello world');
logger.error('hello world');
expect(existsSync(join(tmpLogsDir, 'logs'))).toBeTruthy();
mm.restore();
});
});

0 comments on commit 8a15f69

Please sign in to comment.