error_log

Massimo Melina committed Jan 11, 2022 at 18:36 UTC b25d9302a639e5e3809469bfe2402a12a6fae91d
4 files changed +13 -3
README.md
+2
@@ -36,6 +36,8 @@ Supported entries are:
36 - `port` where to accept http connections. Default is 80.
37 - `vfs` the files and folders you want to expose. For details see the dedicated following section.
38 - `log` path of the log file. Default is `access.log`.
39 +- `error_log` path of the log file for errors. Default is `error.log`.
40 +- `errors_in_main_log` if you want to use a single file for both kind of entries. Default is false.
41 - `accounts` path of the accounts file. Default is `accounts.yaml`.
42 - `mime` command what mime-type to be returned with some files.
43 E.g.: `"*.jpg": image/jpeg`
config.yaml
+2
@@ -1,6 +1,8 @@
1 #port: 80
2 #max_kbps: 1000
3 #max_kbps_per_ip: 500
4 +#log:
5 +#error_log:
6 mime:
7 "*.jpg|*.png|*.mp3|*.txt": auto
8 vfs:
src/log.ts
+9 -2
@@ -1,6 +1,6 @@
1 import Koa from 'koa'
2 import { Writable } from 'stream'
3 -import { subscribeConfig } from './config'
3 +import { getConfig, subscribeConfig } from './config'
4 import { createWriteStream } from 'fs'
5 import * as util from 'util'
6
@@ -16,16 +16,23 @@ class Logger {
16 }
17
18 const accessLogger = new Logger()
19 +const errorLogger = new Logger()
20
21 subscribeConfig({ k: 'log', defaultValue: 'access.log' }, path => {
22 console.debug('log file: ' + (path || 'disabled'))
23 accessLogger.setPath(path)
24 })
25
26 +subscribeConfig({ k: 'error_log', defaultValue: 'error.log' }, path => {
27 + console.debug('error log: ' + (path || 'disabled'))
28 + errorLogger.setPath(path)
29 +})
30 +
31 export function log(): Koa.Middleware {
32 return async (ctx, next) => { // wrapping in a function will make it use current 'mw' value
33 await next()
28 - const st = accessLogger.stream
34 + const isError = ctx.status >= 400
35 + let st = isError && !getConfig('errors_in_main_log') && errorLogger.stream || accessLogger.stream
36 if (!st) return
37 const format = '%s - - [%s] "%s %s HTTP/%s" %d %s\n';
38 const a = new Date().toString().split(' ')
todo.md
-1
@@ -21,4 +21,3 @@
21 - administration interface
22 - log: ip2name
23 - apis in separated log file with parameters?
24 -- errors in separated log file