Dragonfly to support line exclusion

This commit is contained in:
斟酌 鵬兄 2022-04-04 14:56:36 +09:00
parent 38f2911266
commit 9c0b3474e9
3 changed files with 88 additions and 13 deletions

View File

@ -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" );
}
}

View File

@ -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 )
{

View File

@ -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`