Skip to content

Commit 658de95

Browse files
CUSDK-190: Simplified Logger (#151)
* Remove Markdown support in logs. * Removed beautify support for logs. * Removed compact support in logs * Masking support has now moved to Masker class
1 parent 4f756c9 commit 658de95

File tree

14 files changed

+234
-430
lines changed

14 files changed

+234
-430
lines changed

connect/api/impl/ApiClientImpl.hx

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import connect.logger.Logger;
99
import connect.util.Blob;
1010
import connect.util.Collection;
1111
import connect.util.Dictionary;
12+
import connect.util.Masker;
1213
import connect.util.Util;
1314
#if !js
1415
import haxe.io.BytesInput;
@@ -301,28 +302,20 @@ class ApiClientImpl extends Base implements IApiClient {
301302
}
302303

303304
private static function maskHeaders(headers: Dictionary): Dictionary {
304-
return Dictionary.fromObject(Util.maskFields(headers.toObject()));
305+
return Dictionary.fromObject(Masker.maskFields(headers.toObject()));
305306
}
306307

307308
private static function getFormattedData(data: String, title: String, fmt: ILoggerFormatter)
308309
: String {
309-
final compact = Env.getLogger().getLevel() != Logger.LEVEL_DEBUG;
310310
if (Util.isJson(data)) {
311-
final prefix = compact ? '$title (compact): ' : '$title:\n';
312-
final block = fmt.formatCodeBlock(
313-
Env.getLogger().getLevel(),
314-
Util.beautify(
315-
data,
316-
Env.getLogger().isCompact(),
317-
Env.getLogger().getLevel() != Logger.LEVEL_DEBUG,
318-
Env.getLogger().isBeautified()),
319-
'json');
311+
final maskedData = (Env.getLogger().getLevel() != Logger.LEVEL_DEBUG)
312+
? Masker.maskString(data)
313+
: data;
314+
final prefix = '$title:\n';
315+
final block = fmt.formatCodeBlock(Env.getLogger().getLevel(), maskedData, 'json');
320316
return '$prefix$block';
321317
} else {
322-
final fixedBody = compact
323-
? StringTools.lpad(data.substr(data.length - 4), '*', data.length)
324-
: data;
325-
return '$title: $fixedBody';
318+
return '$title: $data';
326319
}
327320
}
328321

connect/flow/FlowLogger.hx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ package connect.flow;
22

33
import connect.logger.ILoggerFormatter;
44
import connect.logger.Logger;
5-
import connect.models.AssetRequest;
65
import connect.models.IdModel;
7-
import connect.models.Listing;
8-
import connect.models.TierConfigRequest;
9-
import connect.models.UsageFile;
106
import connect.util.Collection;
117
import connect.util.DateTime;
128
import connect.util.Dictionary;
9+
import connect.util.Masker;
1310
import connect.util.Util;
1411
import haxe.Json;
1512

@@ -83,13 +80,10 @@ class FlowLogger {
8380
final lastRequestObj = Util.isJsonObject(lastRequest) ? Json.parse(lastRequest) : null;
8481
final requestObj = (Util.isJsonObject(request) && lastRequestObj != null) ? Json.parse(request) : null;
8582
final diff = (lastRequestObj != null && requestObj != null) ? Util.createObjectDiff(requestObj, lastRequestObj) : null;
86-
final requestStr = (diff != null)
87-
? Util.beautifyObject(
88-
diff,
89-
logger.isCompact(),
90-
Env.getLogger().getLevel() != Logger.LEVEL_DEBUG,
91-
logger.isBeautified())
92-
: request;
83+
final requestStr =
84+
(diff == null) ? request :
85+
(Env.getLogger().getLevel() != Logger.LEVEL_DEBUG) ? Masker.maskObject(diff) :
86+
haxe.Json.stringify(diff);
9387
final requestTitle = (diff != null) ? 'Request (changes):' : 'Request:';
9488
return '$requestTitle${fmt.formatCodeBlock(logger.getLevel(),Std.string(requestStr), 'json')}';
9589
} else {

connect/flow/ProcessedRequestInfo.hx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package connect.flow;
22

33
import connect.logger.Logger;
4-
import connect.util.Util;
5-
import connect.util.Dictionary;
64
import connect.models.IdModel;
5+
import connect.util.Dictionary;
6+
import connect.util.Masker;
77

88
@:dox(hide)
99
class ProcessedRequestInfo {
@@ -36,13 +36,13 @@ class ProcessedRequestInfo {
3636
}
3737

3838
private static function requestToString(request:Null<IdModel>):String {
39-
return (request != null)
40-
? Util.beautifyObject(
41-
request.toObject(),
42-
Env.getLogger().isCompact(),
43-
Env.getLogger().getLevel() != Logger.LEVEL_DEBUG,
44-
Env.getLogger().isBeautified())
45-
: '';
39+
if (request != null) {
40+
return (Env.getLogger().getLevel() != Logger.LEVEL_DEBUG)
41+
? Masker.maskObject(request.toObject())
42+
: haxe.Json.stringify(request.toObject());
43+
} else {
44+
return '';
45+
}
4646
}
4747

4848
private static function dataToString(data:Null<Dictionary>):String {

connect/logger/ILoggerFormatter.hx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import connect.util.Collection;
1010

1111
/**
1212
* Represents a log formatter.The `Logger` uses an instance of a class that implements
13-
* this interface (`PlainLoggerFormatter` or `MarkdownLoggerFormatter` by default,
14-
* depending on the value of `LoggerConfig.markdown()`) to write log messages.
13+
* this interface (`PlainLoggerFormatter` by default) to write log messages.
1514
*/
1615
interface ILoggerFormatter {
1716
public function formatSection(level:Int, sectionLevel:Int, text:String):String;

connect/logger/Logger.hx

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import connect.util.Util;
1818
This class is used to log events to a file and the output console.
1919
**/
2020
class Logger extends Base {
21-
/** Only writes compact error level messages. **/
21+
/** Only writes error level messages. **/
2222
public static final LEVEL_ERROR = 0;
2323

24-
/** Only writes compact error & warning level messages. **/
24+
/** Only writes error & warning level messages. **/
2525
public static final LEVEL_WARNING = 1;
2626

27-
/** Only writes compact error & info level messages. **/
27+
/** Only writes error & info level messages. **/
2828
public static final LEVEL_INFO = 2;
2929

3030
/** Writes detailed messages of all levels. Masking of sensitive data is disabled in this level. **/
@@ -37,10 +37,8 @@ class Logger extends Base {
3737
private final maskedFields:Collection<String>;
3838
private final maskedParams:Collection<String>;
3939
private final regexMaskingList:Collection<EReg>;
40-
private final compact:Bool;
41-
private final beautify:Bool;
4240
private var defaultFilename:String;
43-
private final initialConfig: LoggerConfig;
41+
private final initialConfig:LoggerConfig;
4442

4543
/**
4644
Creates a new Logger object. You don't normally create objects of this class,
@@ -57,8 +55,6 @@ class Logger extends Base {
5755
this.maskedParams = config.maskedParams_.copy();
5856
this.regexMaskingList = config.regexMaskingList_.copy();
5957
if (this.maskedFields.indexOf('Authorization') == -1) this.maskedFields.push('Authorization');
60-
this.beautify = config.beautify_;
61-
this.compact = (this.level != LEVEL_DEBUG) ? config.compact_ : false;
6258
this.defaultFilename = null;
6359
}
6460

@@ -82,19 +78,20 @@ class Logger extends Base {
8278

8379
/**
8480
* @return Bool Whether the logs are written in beautified format (this is,
85-
* for JSON objects use new lines and two space indentation).
81+
* for JSON objects use new lines and two space indentation). This method
82+
* is deprecated and always returns false.
8683
*/
8784
public function isBeautified(): Bool {
88-
return this.beautify;
85+
return false;
8986
}
9087

9188
/**
9289
* @return Bool Whether the logs are written in compact format (this is,
9390
* for JSON objects only print key names or, if it has an 'id' field,
94-
* only the id)..
91+
* only the id). This method is deprecated and always returns false.
9592
*/
9693
public function isCompact(): Bool {
97-
return this.compact;
94+
return false;
9895
}
9996

10097
/**
@@ -136,7 +133,7 @@ class Logger extends Base {
136133
final defaultProduct = 'product';
137134
final defaultTierAccount = 'tierAccount';
138135

139-
if(Std.isOfType(request, Asset)){
136+
if (Std.isOfType(request, Asset)) {
140137
final asset = cast(request, Asset);
141138
final provider = asset.connection.provider != null ? asset.connection.provider.id : defaultProvider;
142139
final hub = asset.connection.hub != null ? asset.connection.hub.id : defaultHub;
@@ -146,7 +143,7 @@ class Logger extends Base {
146143
this.setFilename('$provider/$hub/$marketplace/$product/$tierAccount');
147144
}
148145

149-
if(Std.isOfType(request, AssetRequest)){
146+
if (Std.isOfType(request, AssetRequest)) {
150147
final assetRequest = cast(request, AssetRequest);
151148
final provider = assetRequest.asset.connection.provider != null ? assetRequest.asset.connection.provider.id : defaultProvider;
152149
final hub = assetRequest.asset.connection.hub != null ? assetRequest.asset.connection.hub.id : defaultHub;
@@ -156,30 +153,29 @@ class Logger extends Base {
156153
this.setFilename('$provider/$hub/$marketplace/$product/$tierAccount');
157154
}
158155

159-
if(Std.isOfType(request, TierConfigRequest)){
156+
if (Std.isOfType(request, TierConfigRequest)) {
160157
final tierRequest = cast(request, TierConfigRequest);
161158
final provider = tierRequest.configuration.connection.provider != null ? tierRequest.configuration.connection.provider.id : defaultProvider;
162159
final hub = tierRequest.configuration.connection.hub != null ? tierRequest.configuration.connection.hub.id : defaultHub;
163160
final marketplace = tierRequest.configuration.marketplace != null ? tierRequest.configuration.marketplace.id : defaultMarketplace;
164161
final product = tierRequest.configuration.product != null ? tierRequest.configuration.product.id : defaultProduct;
165162
final tierAccount = tierRequest.configuration.account != null ? tierRequest.configuration.account.id : defaultTierAccount;
166-
this.setFilename('$provider/$hub/$marketplace/$product/$tierAccount'); }
167-
163+
this.setFilename('$provider/$hub/$marketplace/$product/$tierAccount');
164+
}
168165

169-
if(Std.isOfType(request, Listing)){
166+
if (Std.isOfType(request, Listing)) {
170167
final listingRequest = cast(request, Listing);
171168
final provider = listingRequest.provider != null ? listingRequest.provider.id : defaultProvider;
172169
final marketplace = listingRequest.contract.marketplace != null ? listingRequest.contract.marketplace.id : defaultMarketplace;
173170
this.setFilename('usage/$provider/$marketplace');
174171
}
175172

176-
if(Std.isOfType(request, UsageFile)){
173+
if (Std.isOfType(request, UsageFile)) {
177174
final usageRequest = cast(request, UsageFile);
178175
final provider = usageRequest.provider != null ? usageRequest.provider.id : defaultProvider;
179176
final marketplace = usageRequest.marketplace.id != null ? usageRequest.marketplace.id : defaultMarketplace;
180177
this.setFilename('usage/$provider/$marketplace');
181178
}
182-
183179
}
184180

185181

@@ -379,9 +375,7 @@ class Logger extends Base {
379375
.level(this.level)
380376
.handlers(handlers)
381377
.maskedFields(this.maskedFields)
382-
.maskedParams(this.maskedParams)
383-
.beautify(this.beautify)
384-
.compact(this.compact);
378+
.maskedParams(this.maskedParams);
385379
final logger = new Logger(config);
386380
Reflect.setField(logger, 'sections', this.sections.copy());
387381
Reflect.setField(logger, 'regexMaskingList', this.regexMaskingList.copy());

connect/logger/LoggerConfig.hx

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,36 @@ import connect.util.Util;
1515
class LoggerConfig extends Base {
1616
@:dox(hide)
1717
public var path_(default, null):String;
18+
1819
@:dox(hide)
1920
public var level_(default, null):Int;
21+
2022
@:dox(hide)
2123
public var handlers_(default, null):Collection<LoggerHandler>;
24+
2225
@:dox(hide)
2326
public var maskedFields_(default, null):Collection<String>;
27+
2428
@:dox(hide)
2529
public var maskedParams_(default, null):Collection<String>;
30+
2631
@:dox(hide)
2732
public var regexMaskingList_:Collection<EReg>;
28-
@:dox(hide)
29-
public var compact_(default, null):Bool;
30-
@:dox(hide)
31-
public var beautify_(default, null):Bool;
3233

3334
private static final levelTranslation:Map<String, Int> = [
3435
'ERROR' => Logger.LEVEL_ERROR,
3536
'WARNING' => Logger.LEVEL_WARNING,
3637
'INFO' => Logger.LEVEL_INFO,
37-
'DEBUG' => Logger.LEVEL_DEBUG];
38-
private var customHandlers:Bool;
38+
'DEBUG' => Logger.LEVEL_DEBUG
39+
];
3940

4041
public function new() {
4142
this.path_ = 'logs';
4243
this.level_ = Logger.LEVEL_INFO;
4344
this.handlers_ = new Collection<LoggerHandler>().push(new LoggerHandler(new PlainLoggerFormatter(), new FileLoggerWriter()));
4445
this.maskedFields_ = new Collection<String>();
4546
this.maskedParams_ = new Collection<String>();
46-
this.compact_ = false;
47-
this.beautify_ = false;
4847
this.regexMaskingList_ = new Collection<EReg>();
49-
this.customHandlers = false;
5048
}
5149

5250
/**
@@ -89,15 +87,13 @@ class LoggerConfig extends Base {
8987
}
9088

9189
/**
92-
* Sets the handlers for the logger. Default is a handler with a plain of Markdown formatter
93-
* (depending on whether `markdown` method was called with a `true` argument) and a file
94-
* writer.
90+
* Sets the handlers for the logger. Default is a handler with a plain formatter
91+
* and a file writer.
9592
* @param handlers Collection of handlers.
9693
* @return LoggerConfig `this` instance to support a fluent interface.
9794
*/
9895
public function handlers(handlers:Collection<LoggerHandler>):LoggerConfig {
9996
this.handlers_ = handlers.copy();
100-
this.customHandlers = true;
10197
return this;
10298
}
10399

@@ -131,27 +127,28 @@ class LoggerConfig extends Base {
131127
}
132128

133129
/**
134-
* Set whether the logs must be written in beautified format (this is,
135-
* for JSON objects use new lines and two space indentation).
130+
* Set whether the logs must be written in beautified format. This method
131+
* is deprecated and it has no effect.
136132
* @param enable Whether beautified logging should be enabled (defaults to `false`).
137133
* @return LoggerConfig `this` instance to support a fluent interface.
138134
*/
139135
public function beautify(enable:Bool):LoggerConfig {
140-
this.beautify_ = enable;
141136
return this;
142137
}
143138

144139
/**
145140
* Sets whether the logs must be written in compact format (this is,
146141
* for JSON objects only prints key names or, if it has an 'id' field,
147-
* only the id). This is ignored if the logger is created in LEVEL_DEBUG.
142+
* only the id). This is ignored if the logger is created in LEVEL_DEBUG. This method
143+
* is deprecated and it has no effect.
148144
* @param enable Whether compact logging should be enabled.
149145
* @return LoggerConfig `this` instance to support a fluent interface.
150146
*/
147+
/*
151148
public function compact(enable:Bool):LoggerConfig {
152-
this.compact_ = enable;
153149
return this;
154150
}
151+
*/
155152

156153
/**
157154
* Set list of regexs to replace in logs strings
@@ -167,19 +164,13 @@ class LoggerConfig extends Base {
167164

168165

169166
/**
170-
* Sets whether the logger should use the Markdown formatter. By default,
171-
* the plain text formatter is used. This property only has effect if no default
172-
* set of logger handlers has been set.
167+
* Sets whether the logger should use the Markdown formatter. This method
168+
* is deprecated and it has no effect. By default, the plain text formatter
169+
* is used.
173170
* @param enable Whether to use the Markdown formatter.
174171
* @return LoggerConfig `this` instance to support a fluent interface.
175172
*/
176173
public function markdown(enable:Bool):LoggerConfig {
177-
if (!this.customHandlers) {
178-
this.handlers_ = new Collection<LoggerHandler>().push(new LoggerHandler(
179-
enable ? new MarkdownLoggerFormatter() : new PlainLoggerFormatter(),
180-
new FileLoggerWriter()
181-
));
182-
}
183174
return this;
184175
}
185176
}

0 commit comments

Comments
 (0)