Skip to content

Commit f4d0000

Browse files
committed
LOGBOX-83 #resolve
Add new LogBox configuration option to disallow serializing complex objects: serializeExtraInfo
1 parent b409308 commit f4d0000

File tree

5 files changed

+79
-33
lines changed

5 files changed

+79
-33
lines changed

system/logging/LogBox.cfc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ component accessors="true" {
182182
category : "ROOT",
183183
levelMin : rootConfig.levelMin,
184184
levelMax : rootConfig.levelMax,
185-
appenders : getAppendersMap( rootConfig.appenders )
185+
appenders : getAppendersMap( rootConfig.appenders ),
186+
serializeExtraInfo : variables.config.getSerializeExtraInfo()
186187
};
187188

188189
// Save in Registry
@@ -250,7 +251,8 @@ component accessors="true" {
250251
category : categoryConfig.name,
251252
levelMin : categoryConfig.levelMin,
252253
levelMax : categoryConfig.levelMax,
253-
appenders : getAppendersMap( categoryConfig.appenders )
254+
appenders : getAppendersMap( categoryConfig.appenders ),
255+
serializeExtraInfo : variables.config.getSerializeExtraInfo()
254256
};
255257
} else {
256258
// Do Category Inheritance? or else just return the root logger.
@@ -259,7 +261,8 @@ component accessors="true" {
259261
args = {
260262
category : arguments.category,
261263
levelMin : root.getLevelMin(),
262-
levelMax : root.getLevelMax()
264+
levelMax : root.getLevelMax(),
265+
serializeExtraInfo : variables.config.getSerializeExtraInfo()
263266
};
264267
}
265268

system/logging/LogEvent.cfc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,29 @@ component accessors="true" {
3131
*/
3232
property name="extrainfo" default="";
3333

34+
/**
35+
* Flag to allow serializing complex objects in `extraInfo`.
36+
*/
37+
property
38+
name ="serializeExtraInfo"
39+
type ="boolean"
40+
default="true";
41+
3442
/**
3543
* Constructor
3644
*
3745
* @message The message to log.
3846
* @severity The severity level to log.
3947
* @extraInfo Extra information to send to the loggers.
4048
* @category The category to log this message under. By default it is blank.
49+
* @serializeExtraInfo Flag to allow serializing complex objects in `extraInfo`. Default is true.
4150
*/
4251
function init(
4352
required message,
4453
required severity,
4554
extraInfo = "",
46-
category = ""
55+
category = "",
56+
boolean serializeExtraInfo = true
4757
){
4858
// Init event
4959
variables.timestamp = now();
@@ -101,6 +111,11 @@ component accessors="true" {
101111
return arrayToList( messageString, chr( 13 ) & chr( 10 ) );
102112
}
103113

114+
// Are we serializing or not?
115+
if ( NOT variables.serializeExtraInfo ) {
116+
return toString( variables.extraInfo );
117+
}
118+
104119
// Component XML conversion
105120
if ( isObject( variables.extraInfo ) ) {
106121
return getXmlConverter().toXML( variables.extraInfo );

system/logging/Logger.cfc

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ component accessors="true" {
3131
*/
3232
property name="levelMax";
3333

34+
/**
35+
* Flag to allow serializing complex objects in `extraInfo`.
36+
*/
37+
property
38+
name ="serializeExtraInfo"
39+
type ="boolean"
40+
default="true";
41+
3442
// The log levels enum as a public property
3543
this.logLevels = new coldbox.system.logging.LogLevels();
3644

@@ -41,17 +49,20 @@ component accessors="true" {
4149
* @levelMin The default log level for this appender, by default it is 0. Optional. ex: LogBox.logLevels.WARN
4250
* @levelMax The default log level for this appender, by default it is 0. Optional. ex: LogBox.logLevels.WARN
4351
* @appenders A struct of already created appenders for this category, or blank to use the root logger.
52+
* @serializeExtraInfo Flag to allow serializing complex objects in `extraInfo`. Default is true.
4453
*/
4554
function init(
4655
required category,
4756
numeric levelMin = 0,
4857
numeric levelMax = 4,
49-
struct appenders = {}
58+
struct appenders = {},
59+
boolean serializeExtraInfo = true
5060
){
5161
// Save Properties
5262
variables.rootLogger = "";
5363
variables.category = arguments.category;
5464
variables.appenders = arguments.appenders;
65+
variables.serializeExtraInfo = arguments.serializeExtraInfo;
5566

5667
// Logger Logging Level defaults, which is wideeeee open!
5768
variables.levelMin = arguments.levelMin;
@@ -61,7 +72,6 @@ component accessors="true" {
6172
variables._hash = createUUID();
6273
variables.util = new coldbox.system.core.util.Util();
6374

64-
6575
// Local Locking
6676
variables.lockName = variables._hash & "LoggerOperation";
6777
variables.lockTimeout = 20;
@@ -320,11 +330,13 @@ component accessors="true" {
320330
* @message A message to log or a closure that returns a message to log
321331
* @severity The severity level to log, if invalid, it will default to **Info**
322332
* @extraInfo Extra information to send to appenders
333+
* @serializeExtraInfo Flag to allow serializing complex objects in `extraInfo`. Default is the logger's setting.
323334
*/
324335
Logger function logMessage(
325336
required message,
326337
required severity,
327-
extraInfo = ""
338+
extraInfo = "",
339+
boolean serializeExtraInfo = variables.serializeExtraInfo
328340
){
329341
var target = this;
330342

@@ -381,17 +393,20 @@ component accessors="true" {
381393
message ="#arguments.message#"
382394
severity ="#arguments.severity#"
383395
extraInfo ="#arguments.extraInfo#"
384-
category ="#arguments.category#" {
396+
category ="#arguments.category#"
397+
serializeExtraInfo="#arguments.serializeExtraInfo#"
398+
{
385399
var target = this;
386400
if ( !hasAppenders() ) {
387401
target = getRootLogger();
388402
}
389403
var thisAppender = target.getAppender( attributes.appenderName );
390404
thread.logEvent = new coldbox.system.logging.LogEvent(
391-
message = attributes.message,
392-
severity = attributes.severity,
393-
extraInfo = attributes.extraInfo,
394-
category = attributes.category
405+
message : attributes.message,
406+
severity : attributes.severity,
407+
extraInfo : attributes.extraInfo,
408+
category : attributes.category,
409+
serializeExtraInfo : attributes.serializeExtraInfo
395410
);
396411
thisAppender.logMessage( thread.logEvent );
397412
}

system/logging/config/LogBoxConfig.cfc

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ component accessors="true" {
2626
*/
2727
property name="rootLogger" type="struct";
2828

29+
/**
30+
* Flag to allow serializing complex objects in `extraInfo`.
31+
*/
32+
property
33+
name ="serializeExtraInfo"
34+
type ="boolean"
35+
default="true";
36+
2937
// The log levels enum as a public property
3038
this.logLevels = new coldbox.system.logging.LogLevels();
3139
// Startup the configuration
@@ -95,39 +103,44 @@ component accessors="true" {
95103
}
96104

97105
// Register Root Logger
98-
if ( NOT structKeyExists( logBoxDSL, "root" ) ) {
106+
if ( !isNull( logBoxDSL.root ) ) {
99107
logBoxDSL.root = { appenders : "*" };
100108
}
101109
root( argumentCollection = logBoxDSL.root );
102110

103111
// Register Categories
104-
if ( structKeyExists( logBoxDSL, "categories" ) ) {
112+
if ( !isNull( logBoxDSL.categories ) ) {
105113
for ( var key in logBoxDSL.categories ) {
106114
logBoxDSL.categories[ key ].name = key;
107115
category( argumentCollection = logBoxDSL.categories[ key ] );
108116
}
109117
}
110118

111119
// Register Level Categories
112-
if ( structKeyExists( logBoxDSL, "debug" ) ) {
120+
if ( !isNull( logBoxDSL.debug ) ) {
113121
DEBUG( argumentCollection = getUtil().arrayToStruct( logBoxDSL.debug ) );
114122
}
115-
if ( structKeyExists( logBoxDSL, "info" ) ) {
123+
if ( !isNull( logBoxDSL.info ) ) {
116124
INFO( argumentCollection = getUtil().arrayToStruct( logBoxDSL.info ) );
117125
}
118-
if ( structKeyExists( logBoxDSL, "warn" ) ) {
126+
if ( !isNull( logBoxDSL.warn ) ) {
119127
WARN( argumentCollection = getUtil().arrayToStruct( logBoxDSL.warn ) );
120128
}
121-
if ( structKeyExists( logBoxDSL, "error" ) ) {
129+
if ( !isNull( logBoxDSL.error ) ) {
122130
ERROR( argumentCollection = getUtil().arrayToStruct( logBoxDSL.error ) );
123131
}
124-
if ( structKeyExists( logBoxDSL, "fatal" ) ) {
132+
if ( !isNull( logBoxDSL.fatal ) ) {
125133
FATAL( argumentCollection = getUtil().arrayToStruct( logBoxDSL.fatal ) );
126134
}
127-
if ( structKeyExists( logBoxDSL, "off" ) ) {
135+
if ( !isNull( logBoxDSL.off ) ) {
128136
OFF( argumentCollection = getUtil().arrayToStruct( logBoxDSL.off ) );
129137
}
130138

139+
// Register serializeExtraInfo
140+
if ( !isNull( logBoxDSL.serializeExtraInfo ) ) {
141+
variables.serializeExtraInfo = logBoxDSL.serializeExtraInfo;
142+
}
143+
131144
return this;
132145
}
133146

tests/specs/logging/LoggerTest.cfc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
component extends="coldbox.system.testing.BaseModelTest" {
22

33
function run(){
4-
describe( "Logger", function(){
5-
beforeEach( function( currentSpec ){
4+
describe( "Logger", () =>{
5+
beforeEach( ( currentSpec ) =>{
66
setup();
77

88
mockLB = createMock( className = "coldbox.system.logging.LogBox", clearMethods = true );
@@ -15,11 +15,11 @@
1515
logger.setRootLogger( rootLogger );
1616
} );
1717

18-
it( "can create the logger", function(){
18+
it( "can create the logger", () =>{
1919
expect( logger ).toBeComponent();
2020
} );
2121

22-
it( "can verify all canX Methods", function(){
22+
it( "can verify all canX Methods", () =>{
2323
logger.setLevelMin( 0 );
2424
logger.setLevelMax( 3 );
2525

@@ -30,7 +30,7 @@
3030
assertFalse( logger.canDebug() );
3131
} );
3232

33-
it( "can verify all canX Methods with values", function(){
33+
it( "can verify all canX Methods with values", () =>{
3434
logger.setLevelMin( 0 );
3535
logger.setLevelMax( 3 );
3636

@@ -40,7 +40,7 @@
4040
assertFalse( logger.canLog( "asdfasfdsd" ) );
4141
} );
4242

43-
it( "can execute all logging methods", function(){
43+
it( "can execute all logging methods", () =>{
4444
// has appenders
4545
assertFalse( logger.hasAppenders() );
4646
// get appenders
@@ -67,7 +67,7 @@
6767
assertFalse( logger.hasAppenders() );
6868
} );
6969

70-
it( "can verify logging levels", function(){
70+
it( "can verify logging levels", () =>{
7171
logger.setLevelMin( 0 );
7272
logger.setLevelMax( 4 );
7373

@@ -97,8 +97,8 @@
9797
assertEquals( 1, arrayLen( newAppender.$callLog().logMessage ) );
9898
} );
9999

100-
describe( "can do logging with closures and automated canX inclusions", function(){
101-
beforeEach( function( currentSpec ){
100+
describe( "can do logging with closures and automated canX inclusions", () =>{
101+
beforeEach( ( currentSpec ) =>{
102102
// MockAppender
103103
mockAppender = createStub()
104104
.$( "getName", "MockAppender" )
@@ -109,20 +109,20 @@
109109
logger.addAppender( mockAppender );
110110
} );
111111

112-
it( "can log with a valid severity", function(){
112+
it( "can log with a valid severity", () =>{
113113
// Test closure in logger
114-
logger.logMessage( function(){
114+
logger.logMessage( () =>{
115115
return "This is a closure message";
116116
}, "info" );
117117
expect( mockAppender.$callLog().logMessage.first()[ "1" ].getMessage() ).toBe(
118118
"This is a closure message"
119119
);
120120
} );
121121

122-
it( "won't log with a non-loggable severity", function(){
122+
it( "won't log with a non-loggable severity", () =>{
123123
logger.setLevelMax( 1 );
124124
// Test closure in logger
125-
logger.logMessage( function(){
125+
logger.logMessage( () =>{
126126
return "This is a closure message";
127127
}, "debug" );
128128
expect( mockAppender.$callLog().logmessage ).toBeEmpty();

0 commit comments

Comments
 (0)