fix: missing user field in access.log

Massimo Melina committed Aug 3, 2022 at 13:00 UTC e79eb11e2ecf75fb0a2c10e3a24af7d5073428ae
2 files changed +5 -3
server/src/log.ts
+5 -2
@@ -10,6 +10,7 @@ import { DAY } from './const'
10 import events from './events'
11 import _ from 'lodash'
12 import { dirname } from 'path'
13 +import { getCurrentUsername } from './perm'
14
15 class Logger {
16 stream?: Writable
@@ -86,11 +87,13 @@ export function log(): Koa.Middleware {
87 stream = logger.reopen() // keep variable updated
88 }
89 }
89 - const format = '%s - - [%s] "%s %s HTTP/%s" %d %s\n';
90 + const format = '%s - %s [%s] "%s %s HTTP/%s" %d %s\n' // Apache's Common Log Format
91 const date = a[2]+'/'+a[1]+'/'+a[3]+':'+a[4]+' '+a[5].slice(3)
91 - events.emit(logger.name, Object.assign(_.pick(ctx, ['ip', 'method','status','length']), { ts: now, uri: ctx.path }))
92 + const user = getCurrentUsername(ctx)
93 + events.emit(logger.name, Object.assign(_.pick(ctx, ['ip', 'method','status','length']), { user, ts: now, uri: ctx.path }))
94 stream.write(util.format( format,
95 ctx.ip,
96 + user || '-',
97 date,
98 ctx.method,
99 ctx.path,
todo.md
-1
@@ -1,6 +1,5 @@
1 # To do
2 - fix: chrome is prompting to save credentials without username because of login's double-form
3 -- fix: currently user field in the access.log is '-'
3 - admin: check + update
4 - admin/monitor: account column
5 - admin/monitor: show total throughput