From 9c0b3474e9faee5c39a6cf9c9ebf5cb23b5f6af5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=9F=E9=85=8C=20=E9=B5=AC=E5=85=84?= Date: Mon, 4 Apr 2022 14:56:36 +0900 Subject: [PATCH] Dragonfly to support line exclusion --- Dragonfly.js | 97 ++++++++++++++++++++++++++++++++++++++++----- net/Http.js | 2 +- system/PageCache.js | 2 +- 3 files changed, 88 insertions(+), 13 deletions(-) diff --git a/Dragonfly.js b/Dragonfly.js index ce1111d..c456c3c 100644 --- a/Dragonfly.js +++ b/Dragonfly.js @@ -1,5 +1,6 @@ "use strict"; -var util = require( "util" ); +const util = require( "util" ); +const cluster = require( "cluster" ); /*{{{ Private methods */ // Months @@ -21,6 +22,51 @@ function logDate( date ) + ":" + padZero( date.getSeconds() ) + " ] "; } + +function test_startsWithAny( line, conds ) +{ + if( conds === undefined ) + return true; + + for( let cond of conds ) + { + if( line.startsWith( cond ) ) + { + return true; + } + } + return false; +} + +function test_endsWithAny( line, conds ) +{ + if( conds === undefined ) + return true; + + for( let cond of conds ) + { + if( line.endsWith( cond ) ) + { + return true; + } + } + return false; +} + +function test_containsAll( line, conds ) +{ + if( conds === undefined ) + return true; + + for( let cond of conds ) + { + if( !~line.indexOf( cond ) ) + { + return false; + } + } + return true; +} /*}}}*/ // Logger class Dragonfly @@ -65,6 +111,7 @@ class Dragonfly this.currentSphere = Dragonfly.defaultSphere; this.Visibility = Dragonfly.Visibility; this.Spheres = Dragonfly.Spheres; + this._configured = false; // Bind prototype functions for( var i in Dragonfly.prototype ) @@ -72,7 +119,6 @@ class Dragonfly Dragonfly[i] = this[i].bind( this ); } - var cluster = require( "cluster" ); if( cluster.isMaster ) { this.logHandler = logHandler || { write: console.log }; @@ -86,10 +132,10 @@ class Dragonfly } else { + // Send to master to prevent multiple process competing to write into the handler this.logHandler = { write: function( e ) { process.send({ cmd: "dragonLog", data: e }); } }; } - var cluster = require("cluster"); var tag = cluster.isMaster ? "M" : "S"; this.ptag = "[ " + tag + ":" + process.pid + " ] "; @@ -140,28 +186,57 @@ class Dragonfly } } - writeLine () + writeLine() { - for( var i in arguments ) + for( let x of arguments ) { - if( typeof( arguments[i] ) == "string" ) + if( typeof( x ) == "string" ) { - this.__log( arguments[i] ); + if( this._exclude( x ) ) + return; + this.__log( x ); } else { - var lines = util.inspect( arguments[i] ).split("\n"); - for( var j in lines ) + var lines = util.inspect( x ); + if( this._exclude( lines ) ) + return; + + for( let line of lines.split("\n") ) { - this.__log( lines[j] ); + this.__log( line ); } } } } + _exclude( line ) + { + if( !this._configured ) + this._configure(); + + for( let ex of this._excludes ) + { + if( test_startsWithAny( line, ex[ "^" ] ) + && test_endsWithAny( line, ex[ "$" ] ) + && test_containsAll( line, ex[ "~" ] ) ) + { + return true; + } + } + return false; + } + + _configure() + { + this._configured = true; + var cl = global.botanLoader; + this._excludes = cl.load( "config.all" ).log.exclude; + } + __log ( line ) { - this.logHandler.write( this.ptag + logDate( new Date() ) + util.format( line ) + "\n" ); + this.logHandler.write( this.ptag + logDate( new Date() ) + line + "\n" ); } } diff --git a/net/Http.js b/net/Http.js index ea2eb84..b5e6f1b 100644 --- a/net/Http.js +++ b/net/Http.js @@ -41,7 +41,7 @@ class CResponse class CRequest { get isPost() { return this.raw.method == 'POST'; } - get remoteAddr() { return this.raw.connection.remoteAddress; } + get remoteAddr() { return this.raw.socket.remoteAddress; } constructor( req, Http ) { diff --git a/system/PageCache.js b/system/PageCache.js index aa40325..db61e1d 100644 --- a/system/PageCache.js +++ b/system/PageCache.js @@ -93,7 +93,7 @@ class PageCache Dragonfly.Info( "[C] " - + ( req.headers[ "x-forwarded-for" ] || req.connection.remoteAddress ) + " " + + ( req.headers[ "x-forwarded-for" ] || req.socket.remoteAddress ) + " " + req.method + ": " + url + " - " + req.headers["user-agent"] + ` in ${process.hrtime.bigint() - res._hrtime}ns`