-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
43 lines (36 loc) · 1.5 KB
/
logger.js
File metadata and controls
43 lines (36 loc) · 1.5 KB
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
// https://youtube.com/watch?v=lWmbK6iZML4
// Pino vs Winston
// - https://betterstack.com/community/comparisons/pino-vs-winston
// - https://dev.to/wallacefreitas/pino-vs-winston-choosing-the-right-logger-for-your-nodejs-application-369n
// Choosing a logging framework - https://betterstack.com/community/guides/logging/logging-framework/
// Logging libraries - https://betterstack.com/community/guides/logging/best-nodejs-logging-libraries
// Structured logging in Node.js with Winston - https://thedreaming.org/2020/06/24/structured-logging-nodejs
console.log ('LOGGERRRRRRRRRRRRRRRR')
const LEVELS = {
error: 0 // highest
, warning: 1
, info: 2
, http: 3
, debug: 4
} // LEVELS
import url from 'url'
function log ( request ) {
let agent = request.get ( 'User-Agent' )
, { method, protocol, hostname, path, query, url } = request
, url_parts = new URL ( url, `${ protocol }://${ hostname }` )
, ip = request.get ( 'x-forwarded-for' ) || request.socket.remoteAddress
console.log ( 'Query: ', query )
return `[${ ip }] `
+ method + ' '
+ `${ protocol }://${ hostname }:${ process.env.PORT }${ url } `
+ 'Path: ' + path + ' | '
+ 'Query Search: ' + url_parts.search.replace ( /\?/, '' ) + ' | '
+ agent
} // log
export default ( location = './index.log' ) =>
( req, res, next ) => {
console.log ( new Date, location, log ( req ) )
res.setHeader ( 'X-Powered-By', 'devPunks' )
res.setHeader ( 'X-Timestamp', `${ Date.now () } (${ new Date })` )
next ( req, res )
} // =>