Permalink
Browse files

feature: Added some predefined 'LogRecord'-transformers

  • Loading branch information...
MikeMitterer committed Jun 28, 2018
1 parent 502e305 commit f8fe021eb7a4578baf5bdfb2be784d00b46fb745
@@ -17,7 +17,7 @@ import "package:console_log_handler/console_log_handler.dart";
import "package:console_log_handler/print_log_handler.dart";
void main() {
configLogging(show: Level.INFO);
configLogging(show: Level.INFO, transformer: transformerMessageOnly);
final Logger _logger = new Logger("test");
try {
@@ -5,7 +5,7 @@ import "package:console_log_handler/print_log_handler.dart";

main() async {
final Logger _logger = new Logger("unit.test.Logging");
configLogging(show: Level.FINE);
configLogging(show: Level.FINE, transformer: transformerMessageOnly);

_logger.fine("Show this debug message");

@@ -50,7 +50,7 @@ class LogConsoleHandler extends LogHandler {

final TransformLogRecord _transformer;

LogConsoleHandler( { final TransformLogRecord transformer: defaultTransformer } )
LogConsoleHandler( { final TransformLogRecord transformer: transformerDefault } )
: _transformer = transformer;

/// More infos about console output:
@@ -33,10 +33,15 @@ void logToConsole(final LogRecord logRecord,{ TransformLogRecord transformer })
/// ...
/// }
///
void configLogging({ final Level show: Level.INFO }) {
void configLogging({ final Level show: Level.INFO,
final TransformLogRecord transformer = transformerDefault }) {

hierarchicalLoggingEnabled = true;

Logger.root.level = show;
Logger.root.onRecord.listen(logToConsole);
Logger.root.onRecord.listen((final LogRecord event)
=> logToConsole(event,transformer: transformer));

}

/// Shows log-messages on the Console
@@ -58,7 +63,7 @@ class LogPrintHandler extends LogHandler {

final bool _supportsColor = supportsColor;

LogPrintHandler( { final TransformLogRecord transformer: defaultTransformer } )
LogPrintHandler( { final TransformLogRecord transformer: transformerDefault } )
: _transformer = transformer;

/// More infos about console output:
@@ -20,46 +20,18 @@
library console_log_handler.shared;

import "dart:convert";
import 'package:logging/logging.dart';
import 'dart:math';
import "package:intl/intl.dart";
import 'package:logging/logging.dart';

part 'transformer.dart';

/// Is called after the console output is done
typedef void MakeConsoleGroup(final LogRecord logRecord);

/// Converts [LogRecord] to String
typedef String TransformLogRecord(final LogRecord logRecord);

/// prettyPrint for JSON
const JsonEncoder PRETTYJSON = const JsonEncoder.withIndent(' ');

String defaultTransformer(final LogRecord logRecord,{ final int nameWidth = 20 }) {
final dateFormat = new DateFormat("HH:mm:ss.SSS");

String loggerName = logRecord.loggerName.substring(max(0,logRecord.loggerName.length - nameWidth));
String shortLoggerName = logRecord.loggerName.replaceAll(new RegExp('^.+\\.'), "");

String time;
if (logRecord.time != null) {
time = dateFormat.format(logRecord.time);
} else {
time = dateFormat.format(new DateTime.now());
}

if(loggerName.length > nameWidth) {
loggerName = shortLoggerName;
}
loggerName += ":";
loggerName = loggerName.padRight(nameWidth);

if (logRecord.error != null) {
return "$time [${logRecord.level}] ${loggerName} ${logRecord.message} / ${logRecord.error}";

} else {
return "$time [${logRecord.level}] ${loggerName} ${logRecord.message}";
}
}

abstract class LogHandler {
static const String GROUP_STACK_TRACE = ' ○ StackTrace';
static const String GROUP_OBJECT = ' ○ Dart-Object';
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2017, Michael Mitterer (office@mikemitterer.at),
* IT-Consulting and Development Limited.
*
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

part of console_log_handler.shared;

/// Converts [LogRecord] to String
typedef String TransformLogRecord(final LogRecord logRecord);

String transformerDefault(final LogRecord logRecord,{ final int nameWidth = 20 }) {
final dateFormat = new DateFormat("HH:mm:ss.SSS");

String loggerName = logRecord.loggerName.substring(max(0,logRecord.loggerName.length - nameWidth));
String shortLoggerName = logRecord.loggerName.replaceAll(new RegExp('^.+\\.'), "");

String time;
if (logRecord.time != null) {
time = dateFormat.format(logRecord.time);
} else {
time = dateFormat.format(new DateTime.now());
}

if(loggerName.length > nameWidth) {
loggerName = shortLoggerName;
}
loggerName += ":";
loggerName = loggerName.padRight(nameWidth);

if (logRecord.error != null) {
return "$time [${logRecord.level}] ${loggerName} ${logRecord.message} / ${logRecord.error}";

} else {
return "$time [${logRecord.level}] ${loggerName} ${logRecord.message}";
}
}

String transformerSimple(final LogRecord logRecord) {
String shortLoggerName = logRecord.loggerName.replaceAll(new RegExp(r'^.+[.]'), "");

if (logRecord.error != null) {
return "${shortLoggerName}: ${logRecord.message} / ${logRecord.error}";

} else {
return "${shortLoggerName}: ${logRecord.message}";
}
}

String transformerMessageOnly(final LogRecord logRecord) {
if (logRecord.error != null) {
return "${logRecord.message} / ${logRecord.error}";

} else {
return "${logRecord.message}";
}
}



0 comments on commit f8fe021

Please sign in to comment.