Skip to content

Commit

Permalink
feat: improve log messages and data (#213)
Browse files Browse the repository at this point in the history
* feat: improve log messages and data

* feat: more logger improvements and log parameter filtering

* feat: add more type information for request logger

* fix: improve startTime accuracy for request logging

* fix: change logger timestamp format to ISO 8601

* style: missing space after const declaration in logger
  • Loading branch information
zacharygolba authored Jul 14, 2016
1 parent 6cd3474 commit a8a5406
Show file tree
Hide file tree
Showing 38 changed files with 717 additions and 370 deletions.
4 changes: 2 additions & 2 deletions decl/ansi-regex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
declare module 'ansi-regex' {
declare function ansiregex(): RegExp;
declare var exports: ansiregex;
declare function ansiRegex(): RegExp;
declare var exports: typeof ansiRegex;
}
2 changes: 2 additions & 0 deletions decl/chalk.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// @flow
declare module 'chalk' {
declare function blue(source: string): string;
declare function cyan(source: string): string;
declare function dim(source: string): string;
declare function green(source: string): string;
declare function magenta(source: string): string;
declare function red(source: string): string;
declare function yellow(source: string): string;
}
4 changes: 3 additions & 1 deletion decl/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ declare module 'http' {
};
}

declare class ServerResponse extends http$ServerResponse {}
declare class ServerResponse extends http$ServerResponse {
stats: Array<Object>;
}

declare function createServer(
handler: (req: IncomingMessage, res: ServerResponse) => void
Expand Down
2 changes: 1 addition & 1 deletion decl/moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ declare module 'moment' {
}

declare function moment(source: Date | number | string | void): Moment;
declare var exports: moment;
declare var exports: typeof moment;
}
3 changes: 3 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// @flow
import { worker, isWorker } from 'cluster';

const { env: ENV } = process;

export const CWD = process.cwd();
export const PID = (isWorker ? worker : process).pid;
export const PORT = parseInt(ENV.PORT, 10) || 4000;
export const NODE_ENV = ENV.NODE_ENV || 'development';
export const LUX_CONSOLE = ENV.LUX_CONSOLE || false;
13 changes: 7 additions & 6 deletions src/packages/application/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type Server from '../server';
import type Controller from '../controller';
import type Serializer from '../serializer';
import typeof { Model } from '../database';
import type { Logger$config } from '../logger/interfaces';

/**
* The `Application` class is responsible for constructing an application and
Expand Down Expand Up @@ -117,20 +118,20 @@ class Application {
* It is highly reccomended that you do not override this method.
*/
constructor({
log = true,
path,
port,
logging,
database
}: {
log: boolean,
path: string,
port: number,
database: {}
path: string;
port: number;
logging: Logger$config;
database: {};
} = {}): Promise<Application> {
return initialize(this, {
log,
path,
port,
logging,
database
});
}
Expand Down
16 changes: 7 additions & 9 deletions src/packages/application/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,28 @@ import {
} from './errors';

import type Application from './index';
import type { Logger$config } from '../logger/interfaces';

/**
* @private
*/
export default async function initialize(app: Application, {
log,
path,
port,
logging,
database
}: {
log: boolean,
path: string,
port: number,
database: {}
path: string;
port: number;
logging: Logger$config;
database: {};
} = {}): Promise<Application> {
const routes = loader(path, 'routes');
const models = loader(path, 'models');
const controllers = loader(path, 'controllers');
const serializers = loader(path, 'serializers');

const logger = await new Logger({
path,
enabled: log
});
const logger = new Logger(logging);

const store = await new Database({
path,
Expand Down
3 changes: 1 addition & 2 deletions src/packages/cli/commands/dbmigrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export async function dbmigrate() {
path: CWD,
checkMigrations: false,

logger: await new Logger({
path: CWD,
logger: new Logger({
enabled: false
})
});
Expand Down
3 changes: 1 addition & 2 deletions src/packages/cli/commands/dbrollback.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export async function dbrollback() {
path: CWD,
checkMigrations: false,

logger: await new Logger({
path: CWD,
logger: new Logger({
enabled: false
})
});
Expand Down
3 changes: 1 addition & 2 deletions src/packages/cli/commands/dbseed.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ export async function dbseed() {
models,
path: CWD,

logger: await new Logger({
path: CWD,
logger: new Logger({
enabled: false
})
});
Expand Down
7 changes: 3 additions & 4 deletions src/packages/cli/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CWD, PORT, NODE_ENV } from '../../../constants';

import Logger from '../../logger';
import Watcher from '../../watcher';
import loader from '../../loader';
import { createCluster } from '../../pm';

import { build } from './build';
Expand All @@ -21,10 +22,8 @@ export async function serve({
cluster: boolean;
useStrict: boolean;
}): Promise<void> {
const logger = await new Logger({
path: CWD,
enabled: true
});
const { logging } = loader(CWD, 'config');
const logger = new Logger(logging);

if (hot) {
const watcher = await new Watcher(CWD);
Expand Down
11 changes: 10 additions & 1 deletion src/packages/cli/templates/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ import template from '../../template';
* @private
*/
export default (name: string, env: string): string => {
const isTestENV = env === 'test';
const isProdENV = env === 'production';

return template`
export default {
log: ${!isProdENV}
logging: {
level: ${isProdENV ? 'INFO' : 'DEBUG'},
format: ${isProdENV ? 'json' : 'text'},
enabled: ${(!isTestENV).toString()},
filter: {
params: []
}
}
};
`;
};
3 changes: 2 additions & 1 deletion src/packages/database/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// @flow
import { ModelMissingError } from './errors';

import Logger from '../logger';
import Model from './model';

import initialize from './initialize';

import type Logger from '../logger';

/**
* @private
*/
Expand Down
53 changes: 13 additions & 40 deletions src/packages/database/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,6 @@ class Model {
primaryKey,
table,

store: {
debug
},

hooks: {
afterUpdate,
afterSave,
Expand All @@ -263,6 +259,8 @@ class Model {
Object.assign(this, attributes);

if (this.isDirty) {
const { constructor: { logger } } = this;

if (typeof beforeValidation === 'function') {
await beforeValidation(this);
}
Expand All @@ -285,15 +283,10 @@ class Model {

const query = table()
.where({ [primaryKey]: this[primaryKey] })
.update(this.format('database', ...this.dirtyAttributes));

if (debug) {
const { constructor: { logger } } = this;

query.on('query', () => {
setImmediate(() => logger.info(sql`${query.toString()}`));
.update(this.format('database', ...this.dirtyAttributes))
.on('query', () => {
setImmediate(() => logger.debug(sql`${query.toString()}`));
});
}

await query;

Expand All @@ -315,12 +308,9 @@ class Model {
const {
constructor: {
primaryKey,
logger,
table,

store: {
debug
},

hooks: {
afterDestroy,
beforeDestroy
Expand All @@ -334,19 +324,10 @@ class Model {

const query = table()
.where({ [primaryKey]: this[primaryKey] })
.del();

if (debug) {
const {
constructor: {
logger
}
} = this;

query.on('query', () => {
setImmediate(() => logger.info(sql`${query.toString()}`));
.del()
.on('query', () => {
setImmediate(() => logger.debug(sql`${query.toString()}`));
});
}

await query;

Expand Down Expand Up @@ -400,12 +381,9 @@ class Model {
static async create(props = {}): Model {
const {
primaryKey,
logger,
table,

store: {
debug
},

hooks: {
afterCreate,
afterSave,
Expand Down Expand Up @@ -443,15 +421,10 @@ class Model {

const query = table()
.returning(primaryKey)
.insert(omit(instance.format('database'), primaryKey));

if (debug) {
const { logger } = this;

query.on('query', () => {
setImmediate(() => logger.info(sql`${query.toString()}`));
.insert(omit(instance.format('database'), primaryKey))
.on('query', () => {
setImmediate(() => logger.debug(sql`${query.toString()}`));
});
}

Object.assign(instance, {
[primaryKey]: (await query)[0]
Expand Down
2 changes: 1 addition & 1 deletion src/packages/database/query/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ class Query {

if (model.store.debug) {
records.on('query', () => {
setImmediate(() => model.logger.info(sql`${records.toString()}`));
setImmediate(() => model.logger.debug(sql`${records.toString()}`));
});
}

Expand Down
30 changes: 0 additions & 30 deletions src/packages/logger/ansi-remover/index.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/packages/logger/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @flow
import type { Logger$level } from './interfaces';

export const DEBUG = 'DEBUG';
export const INFO = 'INFO';
export const WARN = 'WARN';
export const ERROR = 'ERROR';
export const LEVELS: Map<Logger$level, number> = new Map([
[DEBUG, 0],
[INFO, 1],
[WARN, 2],
[ERROR, 3],
]);
Loading

0 comments on commit a8a5406

Please sign in to comment.