Skip to content

Commit

Permalink
Addressing various issues including #6
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbu committed Nov 13, 2013
1 parent 48abe28 commit 7ff9e20
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 73 deletions.
3 changes: 1 addition & 2 deletions build.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:web_ui/component_build.dart';
import 'dart:io';

void main() {
build(new Options().arguments, ['example/browser/test.html']);
// build(new Options().arguments, ['example/browser/test.html']);
}
7 changes: 4 additions & 3 deletions example/browser/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import 'dart:async';

main() {
hierarchicalLoggingEnabled = true;
Logger.root.level = Level.OFF;
var logger = new Logger("mylogger")..level = Level.ALL;
new Logger("loggerui")..level = Level.ALL;
Logger.root.level = Level.ALL;
new Logger("loggerui");//..level = Level.ALL;


attachXLoggerUi();


query("#click").onClick.listen((_) {
querySelector("#click").onClick.listen((_) {
logger.info("Button clicked");
debug("Foo", "loggerui");
});
Expand Down
6 changes: 3 additions & 3 deletions example/browser/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<html>
<head>
<title>test</title>
<link rel="import" href="package:logging_handlers/src/client/loggerui.html">
<link rel="import" href="packages/logging_handlers/src/client/loggerui.html">
</head>

<body>
<button id="click">Click Me</button>
<x-loggerui id="loggerui"></x-loggerui>
<log-loggerui id="loggerui"></log-loggerui>
<script type="application/dart" src="test.dart"></script>
<!-- for this next line to work, your pubspec.yaml file must have a dependency on 'browser' -->
<script src="packages/browser/dart.js"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion example/server/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import 'package:logging/logging.dart';

main() {
var logger = new Logger("mylogger");
logger.onRecord.listen(new PrintHandler());
logger.onRecord.listen(new LogPrintHandler());
logger.info("Hello World"); // should output to the console
}
4 changes: 2 additions & 2 deletions lib/browser_logging_handlers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export 'logging_handlers_shared.dart';
*/
void attachXLoggerUi([bool addPrintHandler=true]) {
Timer.run(() {
var loggerComponents = queryAll("div[is=x-loggerui]");
var loggerComponents = querySelectorAll("div[is=x-loggerui]");
print(loggerComponents);
var listener = Logger.root.onRecord.asBroadcastStream();
loggerComponents.forEach((component) {
Expand All @@ -23,6 +23,6 @@ void attachXLoggerUi([bool addPrintHandler=true]) {
});

//optionally attach a default print handler
if (addPrintHandler) listener.listen(new PrintHandler());
if (addPrintHandler) listener.listen(new LogPrintHandler());
});
}
10 changes: 5 additions & 5 deletions lib/logging_handlers_shared.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library logging_handlers_shared;

import 'dart:json';
import 'dart:convert';

import 'package:logging/logging.dart';
import 'package:intl/intl.dart';
Expand Down Expand Up @@ -36,7 +36,7 @@ log(String message, Level level, [String loggerName]) {
/// quick replacement for `print()` - use `info()` or `debug()`
startQuickLogging() {
hierarchicalLoggingEnabled = true;
Logger.root.onRecord.listen(new PrintHandler());
Logger.root.onRecord.listen(new LogPrintHandler());
info("Quick'n'Dirty logging is enabled. Better to do it properly, though.");
}

Expand All @@ -63,7 +63,7 @@ startQuickLogging() {
* logger.info("Hello World");
* // 2013-04-26 11:50:40.506 mylogger [INFO]: Hello World
*
* The [PrintHandler] constr uctor takes the following optional parameters:
* The [LogPrintHandler] constr uctor takes the following optional parameters:
*
* - [messageFormat] - The format string for log messages.
* This defaults to [StringTransformer.DEFAULT_MESSAGE_FORMAT].
Expand All @@ -79,15 +79,15 @@ startQuickLogging() {
* Check out the [StringTransformer] class for possible format strings.
*
*/
class PrintHandler implements BaseLoggingHandler {
class LogPrintHandler implements BaseLoggingHandler {

LogRecordTransformer transformer;
String messageFormat;
String exceptionFormatSuffix;
String timestampFormat;
Function printFunc;

PrintHandler({
LogPrintHandler({
this.messageFormat: StringTransformer.DEFAULT_MESSAGE_FORMAT,
this.exceptionFormatSuffix: StringTransformer.DEFAULT_EXCEPTION_FORMAT,
this.timestampFormat: StringTransformer.DEFAULT_DATE_TIME_FORMAT,
Expand Down
19 changes: 19 additions & 0 deletions lib/src/browser/logviewer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
library logviewer;

import 'package:polymer/polymer.dart';
import 'package:logging_handlers/logging_handlers_shared.dart';

@CustomTag('log-viewer')
class LogViewer extends PolymerElement {

@observable
List<String> messages = toObservable(new List<String>());

LogViewer.created() : super.created();

LogRecordTransformer transformer = new StringTransformer();

void call(LogRecord logRecord) {
messages.add(transformer.transform(logRecord));
}
}
9 changes: 9 additions & 0 deletions lib/src/browser/logviewer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<polymer-element name="log-viewer">
<template>
Logging:
<template repeat="{{messages}}">
{{}}<br />
</template>
</template>
<script type="application/dart" src="logviewer.dart"></script>
</polymer-element>
17 changes: 7 additions & 10 deletions lib/src/client/loggerui.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
library loggerui;

import 'dart:html';
import 'package:web_ui/web_ui.dart';
import 'package:web_ui/watcher.dart' as watcher;
import 'package:polymer/polymer.dart';
import 'package:logging/logging.dart';
import 'package:logging_handlers/logging_handlers_shared.dart';

class LoggerUi extends WebComponent implements BaseLoggingHandler {
List<LogRecord> logRecords = new List<LogRecord>();
@CustomTag('log-loggerui')
class LoggerUi extends PolymerElement {
List<LogRecord> logRecords = toObservable(new List<LogRecord>());

List<String> messages = new List<String>();
List<String> messages = toObservable(new List<String>());

final _logger = new Logger("loggerui");

LoggerUi() {
transformer = new StringTransformer();
}
LoggerUi.created() : super.created();

LogRecordTransformer transformer;
LogRecordTransformer transformer = new StringTransformer();

void call(LogRecord logRecord) {
if (logRecord.loggerName != "loggerui") _logger.finest("adding logrecord"); // don;t log our own records
logRecords.add(logRecord);
messages.add(transformer.transform(logRecord));
watcher.dispatch();
if (logRecord.loggerName != "loggerui") _logger.finest("logrecord added");
}

Expand Down
17 changes: 6 additions & 11 deletions lib/src/client/loggerui.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
<!DOCTYPE html>

<html>
<body>
<element name="x-loggerui" constructor="LoggerUi" extends="div">
<polymer-element name="log-loggerui">
<template>
<style>
#loggerbox {
Expand All @@ -16,13 +12,12 @@
padding:0px;
}
</style>
<div id="loggerbox" >
<template iterate="message in messages">
<pre class="loggerline">{{ message }}</pre>
<h2>Log messages</h2>
<div id="loggerbox">
<template repeat="{{messages}}">
<pre class="loggerline">{{}}</pre>
</template>
</div>
</template>
<script type="application/dart" src="loggerui.dart"></script>
</element>
</body>
</html>
</polymer-element>
3 changes: 1 addition & 2 deletions lib/src/shared/map_transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class MapTransformer implements LogRecordTransformer {
map["time"] = logRecord.time != null ? logRecord.time.toString() : null;
map["sequenceNumber"] = logRecord.sequenceNumber;
map["loggerName"] = logRecord.loggerName;
map["exceptionText"] = logRecord.exceptionText;
map["exception"] = logRecord.exception != null ? logRecord.exception.toString() : null;
map["exception"] = logRecord.error != null ? logRecord.error .toString() : null;
return map;
}
}
11 changes: 5 additions & 6 deletions lib/src/shared/string_transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class StringTransformer implements LogRecordTransformer {
static const DEFAULT_EXCEPTION_FORMAT = "\n%e\n%x";

/// Default date time format for log messages
static const DEFAULT_DATE_TIME_FORMAT = "yyyy.mm.dd HH:mm:ss.SSS Z";
static const DEFAULT_DATE_TIME_FORMAT = "yyyy.mm.dd HH:mm:ss.SSS";

/// Contains the standard message format string
final String messageFormat;
Expand Down Expand Up @@ -62,7 +62,7 @@ class StringTransformer implements LogRecordTransformer {
* [exceptionFormatSuffix] and [timestampFormat] pattern.
*/
String transform(LogRecord logRecord) {
var formatString = logRecord.exception == null ?
var formatString = logRecord.error == null ?
messageFormat :
messageFormat+exceptionFormatSuffix;

Expand Down Expand Up @@ -91,11 +91,10 @@ class StringTransformer implements LogRecordTransformer {
break;
case SEQ:
return logRecord.sequenceNumber.toString();
case EXCEPTION:
if (logRecord.exception != null) return logRecord.exception.toString();
break;
case EXCEPTION:
case EXCEPTION_TEXT:
return logRecord.exceptionText;
if (logRecord.error != null) return logRecord.error.toString();
break;
}
}

Expand Down
1 change: 1 addition & 0 deletions test/server/mylog1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2013.44.13 09:44:06.409 mylogger [INFO]: Hello World
7 changes: 4 additions & 3 deletions test/shared/shared_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ library shared_test;
import 'package:logging_handlers/logging_handlers_shared.dart';
import 'package:unittest/unittest.dart';
import 'package:logging/logging.dart';
import 'dart:json';
import 'dart:convert';
import 'package:intl/intl.dart';

part 'src/transformer_tests.dart';

Expand All @@ -21,7 +22,7 @@ runTopLevelTests() {
var logger = new Logger("mylogger");
var printed = "";
var printFunc = (value) => printed = value;
logger.onRecord.listen(new PrintHandler(messageFormat:"%m", printFunc:printFunc));
logger.onRecord.listen(new LogPrintHandler(messageFormat:"%m", printFunc:printFunc));
logger.info("Hello World");
expect(printed, equals("Hello World"));
});
Expand All @@ -30,7 +31,7 @@ runTopLevelTests() {
var logger = new Logger("mylogger");
var printed = "";
var printFunc = (value) => printed = value;
logger.onRecord.listen(new PrintHandler(printFunc:printFunc));
logger.onRecord.listen(new LogPrintHandler(printFunc:printFunc));
//logger.onRecord.listen(printHandler(printFunc:printFunc));
logger.info("Hello World");
// contains, rather than equals, because it contains the always-changing timestamp
Expand Down
Loading

0 comments on commit 7ff9e20

Please sign in to comment.