-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
81 lines (73 loc) · 2.32 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { ConnInfo } from './dev_depts.ts';
import { FileLogger } from './transports/filelogger.ts';
export enum LogLevel {
error = 'error',
warn = 'warn',
trace = 'trace',
info = 'info',
log = 'log',
}
export interface Transporter {
callback?: (log: string) => void;
}
/**
* Logger options
* @property ip
* @property level - Log level
* @property userAgent
* @property filename
* @property transports
*/
export interface LoggerOptions {
transports?: Transporter[];
ip?: boolean;
level?: LogLevel;
userAgent?: boolean;
filename?: string;
}
const generateLog =
(req: Request, res: Response, connInfo?: ConnInfo) =>
(logArray: (string | number)[], options?: LoggerOptions) => {
const url = new URL(req.url);
logArray.push('[' + (options?.level ?? LogLevel.log).toUpperCase() + ']');
if (options?.ip) {
logArray.push((connInfo!.remoteAddr as Deno.NetAddr)?.hostname ?? '');
}
logArray.push(res.status);
logArray.push(req.method.toUpperCase());
logArray.push(url.pathname);
logArray.push(url.protocol.slice(0, -1));
if (options?.userAgent) logArray.push(req.headers.get('user-agent') ?? '');
};
/**
* @callback callback
* @param {string} log
* @returns
*/
/**
* @param {boolean} options.ip displays the IP address of the request
* @param {string} options.level - displays the level
* @param {string=} options.filename - file to output logs
* @param {boolean} options.userAgent - displays the user-agent from header if it exists
* @param {Object[]} options.transporter - list of outputs to use
* @param {callback} options.transporter[].callback - callback function
* @returns void
*/
export const logger = (options?: LoggerOptions) => {
const defaultOptions: Transporter[] = [{ callback: console.log }];
const output = options?.transports ?? defaultOptions;
let filelogger: FileLogger;
if (options?.filename) {
filelogger = new FileLogger(options.filename);
}
const callBackList = output
.filter((o) => !!o.callback)
.map((o) => o.callback);
return (req: Request, res: Response, connInfo?: ConnInfo) => {
const args: string[] = [];
generateLog(req, res, connInfo)(args, options);
const logString = args.filter((x) => x.length).join(' ');
if (filelogger) filelogger.toFile(logString);
callBackList.forEach((cb) => cb!(logString));
};
};