-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging.ts
72 lines (61 loc) · 2.13 KB
/
logging.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
import { join } from "https://deno.land/[email protected]/path/mod.ts";
interface LoggingConfig {
logDir: string;
infoLog: string;
errorLog: string;
}
interface Config {
logging: LoggingConfig;
}
const config: Config = JSON.parse(await Deno.readTextFile('config.json'));
const infoPath = join(config.logging.logDir, config.logging.infoLog);
const errorPath = join(config.logging.logDir, config.logging.errorLog);
// interface for a access log
export interface AccessLog {
method: string,
url: string,
protocol: string,
status: number,
size: number,
referer: string,
userAgent: string,
responseTime: number,
upstream_response_time: number,
backend: string,
}
export function requestLoggingPermissions()
{
// request r/w permissions if needed
const readPerm = Deno.permissions.requestSync({name: "read"});
const writePerm = Deno.permissions.requestSync({name: "write"});
if (readPerm.state === "denied") console.log("Deno does not have permission to read. Logging may be impossible.\n");
if (writePerm.state === "denied") console.log("deno does not have permission to write. Editing or creating new log filels may be impossible.\n")
}
// basic logging function
export async function log(logType: string, message: string, context: object = {}) {
const entry = {
timestamp: new Date().toISOString(),
logType,
message,
...context,
};
// if the logs directory does not exist, create it
await Deno.mkdir(config.logging.logDir, { recursive: true });
try {
switch(logType) {
case "INFO":
console.log(entry);
await Deno.writeTextFile(infoPath, JSON.stringify(entry) + "\n", { append: true });
break;
case "ERROR":
console.error(entry);
await Deno.writeTextFile(errorPath, JSON.stringify(entry) + "\n", { append: true });
break;
default:
throw new Error(`Unknown log type: ${logType}`);
}
} catch (error) {
console.error(`Failed to write log entry: ${error}`);
}
}
export default log;