aboutsummaryrefslogtreecommitdiff
path: root/src/logger.ts
diff options
context:
space:
mode:
authorcjd242018-04-19 20:48:19 +0100
committercjd242018-04-19 20:48:19 +0100
commit92e23d37f843e49eda01a4d0ea7ed8005efffdb2 (patch)
tree7c08678dfce9bf82eaa4f71f5e23182f8515d386 /src/logger.ts
parentAdded a write of the base teir JSON object, made the images actually write to... (diff)
parentMerge branch 'trh/gets' into cjd24/conversion (diff)
downloadmedicine-92e23d37f843e49eda01a4d0ea7ed8005efffdb2.tar.gz
medicine-92e23d37f843e49eda01a4d0ea7ed8005efffdb2.zip

Merge branch ‘cjd24/conversion’ of https://gitlab.cs.st-andrews.ac.uk/cs3099group-be-4/project-code into cjd24/conversion

Diffstat (limited to 'src/logger.ts')
-rw-r--r--src/logger.ts231
1 files changed, 185 insertions, 46 deletions
diff --git a/src/logger.ts b/src/logger.ts
index bbb0a73..74ae1f7 100644
--- a/src/logger.ts
+++ b/src/logger.ts
@@ -4,27 +4,107 @@ import * as colors from 'colors/safe';
import * as winston from 'winston';
import { files } from './files';
+import { QueryOptions } from 'sequelize';
-type LogLevel = 'critical' | 'error' | 'warning' | 'security' | 'info';
+type Colour = (s: string) => string;
+
+type LogLevel =
+ | '7'
+ | 'debug'
+ | 'minor'
+ | 'silly'
+ | 'testing'
+ | 'verbose'
+ | '6'
+ | 'information'
+ | 'inform'
+ | 'info'
+ | '5'
+ | 'note'
+ | 'notice'
+ | 'notify'
+ | 'success'
+ | 'security'
+ | '4'
+ | 'warn'
+ | 'warning'
+ | '3'
+ | 'err'
+ | 'failure'
+ | 'fail'
+ | 'error'
+ | '2'
+ | 'crit'
+ | 'severe'
+ | 'critical'
+ | '1'
+ | 'alert'
+ | 'breach'
+ | '0'
+ | 'fatal'
+ | 'emerg'
+ | 'emergency'
+ | 'shutdown';
const LOG_LEVELS: winston.LoggerOptions = {
- info: 4,
- security: 3,
- warning: 2,
- error: 1,
- critical: 0
+ 7: 7,
+ debug: 7,
+ minor: 7,
+ silly: 7,
+ testing: 7,
+ verbose: 7,
+ 6: 6,
+ information: 6,
+ inform: 6,
+ info: 6,
+ 5: 5,
+ note: 5,
+ notice: 5,
+ notify: 5,
+ success: 5,
+ security: 5,
+ 4: 4,
+ warn: 4,
+ warning: 4,
+ 3: 3,
+ err: 3,
+ failure: 3,
+ fail: 3,
+ error: 3,
+ 2: 2,
+ crit: 2,
+ severe: 2,
+ critical: 2,
+ 1: 1,
+ alert: 1,
+ breach: 1,
+ 0: 0,
+ fatal: 0,
+ emerg: 0,
+ emergency: 0,
+ shutdown: 0
};
-const LOG_LEVEL_COLOURS: {
- [key: string]: (s: string) => string;
+const LEVEL_COLOURS: {
+ [key: number]: Colour;
} = {
- info: colors.cyan,
- security: colors.blue,
- warning: colors.yellow,
- error: colors.red,
- critical: colors.red
+ 7: colors.cyan,
+ 6: colors.blue,
+ 5: colors.green,
+ 4: colors.yellow,
+ 3: colors.red,
+ 2: colors.magenta,
+ 1: colors.black,
+ 0: colors.white
};
+const LOG_LEVEL_COLOURS: {
+ [key: string]: Colour;
+} = {};
+Object.keys(LOG_LEVELS).forEach((k: string): void => {
+ LOG_LEVEL_COLOURS[k] = LEVEL_COLOURS[LOG_LEVELS[k]];
+});
+
interface LoggingMetadata {
component?: string;
user?: string;
@@ -36,8 +116,8 @@ export let enabled: boolean = true;
const consoleTransport: winston.TransportInstance =
new winston.transports.Console({
name: 'consoleTransport',
- level: 'info',
- silent: false,
+ level: 'verbose',
+ silent: process.env.NODE_ENV === 'test',
handleExceptions: true,
humanReadableUncaughtException: true,
formatter: (
@@ -58,7 +138,7 @@ const consoleTransport: winston.TransportInstance =
const basicFileTransport: winston.TransportInstance =
new winston.transports.File({
name: 'basicFileTransport',
- level: 'info',
+ level: 'debug',
label: 'Basic File Transport',
silent: false,
colorize: false,
@@ -113,7 +193,7 @@ const criticalFileTransport: winston.TransportInstance =
const queryFileTransport: winston.TransportInstance =
new winston.transports.File({
name: 'queryFileTransport',
- level: 'info',
+ level: 'debug',
label: 'Query Logs',
silent: false,
colorize: false,
@@ -173,6 +253,11 @@ const makeLogFunction: (level: LogLevel) => LogFunction = (
};
};
+function forwardLog(level: LogLevel, message: string, meta: LoggingMetadata)
+ : any {
+ basicLogger.log(level, message, meta);
+}
+
interface FetchParams {
before?: Date;
after?: Date;
@@ -186,54 +271,108 @@ interface LogItem {
timestamp: string;
}
-const fetchLogs: (level: LogLevel, params?: FetchParams) => LogItem[] = (
- level: LogLevel, params: FetchParams = {}
-): LogItem[] => {
- const matches: (log: any, level: LogLevel, params?: FetchParams) => boolean = (
- log: any, minLevel: LogLevel, fetchParams: FetchParams = {}
- ): boolean => {
- const time: Date = new Date(log.timestamp);
- if (params.before && params.before < time) {
- return false;
- } else if (params.after && params.after > time) {
- return false;
- } else {
- return true;
- }
+/* tslint:disable */
+const fetchLogs2: (level?: LogLevel, params?: FetchParams) => Promise<LogItem[]> = (level?: LogLevel, params: FetchParams = {}): Promise<LogItem[]> => {
+ const options: winston.QueryOptions = {
+ from: params.after,
+ until: params.before,
+ start: 0,
+ order: 'desc',
+ fields: ['user', 'component', 'level', 'message', 'label', 'timestamp']
};
- const out: LogItem[] = [];
- basicLogger.stream({ level }).on('log', (log: any): void => {
- if (matches(log, level, params)) {
- out.push({
- component: log.meta.component,
- level: log.level,
- value: log.message,
- username: log.user,
- timestamp: log.timestamp
- });
- }
+
+ return new Promise((res, rej): void => {
+ basicLogger.query(options, function (err, results) {
+ if (err) {
+ /* TODO: handle me */
+ throw err;
+ }
+ res(results.basicFileTransport);
+ });
});
- return out;
};
+/* tslint:enable */
export const logger: {
+ 7: LogFunction;
+ debug: LogFunction;
+ minor: LogFunction;
+ silly: LogFunction;
+ testing: LogFunction;
+ verbose: LogFunction;
+ 6: LogFunction;
+ information: LogFunction;
+ inform: LogFunction;
info: LogFunction;
+ 5: LogFunction;
+ note: LogFunction;
+ notice: LogFunction;
+ notify: LogFunction;
+ success: LogFunction;
security: LogFunction;
+ 4: LogFunction;
+ warn: LogFunction;
warning: LogFunction;
+ 3: LogFunction;
+ err: LogFunction;
+ failure: LogFunction;
+ fail: LogFunction;
error: LogFunction;
+ 2: LogFunction;
+ crit: LogFunction;
+ severe: LogFunction;
critical: LogFunction;
+ 1: LogFunction;
+ alert: LogFunction;
+ breach: LogFunction;
+ 0: LogFunction;
+ fatal: LogFunction;
+ emerg: LogFunction;
+ emergency: LogFunction;
+ shutdown: LogFunction;
isEnabled: () => boolean;
forward: (level: LogLevel, message: string, meta: LoggingMetadata) => void;
- fetch: (level: LogLevel, params?: FetchParams) => LogItem[];
+ fetch: (level: LogLevel, params?: FetchParams) => Promise<LogItem[]>;
} = {
+ 7: makeLogFunction('7'),
+ debug: makeLogFunction('debug'),
+ minor: makeLogFunction('minor'),
+ silly: makeLogFunction('silly'),
+ testing: makeLogFunction('testing'),
+ verbose: makeLogFunction('verbose'),
+ 6: makeLogFunction('6'),
+ information: makeLogFunction('information'),
+ inform: makeLogFunction('inform'),
info: makeLogFunction('info'),
+ 5: makeLogFunction('5'),
+ note: makeLogFunction('note'),
+ notice: makeLogFunction('notice'),
+ notify: makeLogFunction('notify'),
+ success: makeLogFunction('success'),
security: makeLogFunction('security'),
+ 4: makeLogFunction('4'),
+ warn: makeLogFunction('warn'),
warning: makeLogFunction('warning'),
+ 3: makeLogFunction('3'),
+ err: makeLogFunction('err'),
+ failure: makeLogFunction('failure'),
+ fail: makeLogFunction('fail'),
error: makeLogFunction('error'),
+ 2: makeLogFunction('2'),
+ crit: makeLogFunction('crit'),
+ severe: makeLogFunction('severe'),
critical: makeLogFunction('critical'),
+ 1: makeLogFunction('1'),
+ alert: makeLogFunction('alert'),
+ breach: makeLogFunction('breach'),
+ 0: makeLogFunction('0'),
+ fatal: makeLogFunction('fatal'),
+ emerg: makeLogFunction('emerg'),
+ emergency: makeLogFunction('emergency'),
+ shutdown: makeLogFunction('shutdown'),
isEnabled: (): boolean => !basicFileTransport.silent,
- forward: basicLogger.log,
- fetch: fetchLogs
+ forward: forwardLog,
+ fetch: fetchLogs2
};
export const logQuery: (query: string, duration: string) => void = (