Skip to content

Commit

Permalink
use class mirrors
Browse files Browse the repository at this point in the history
  • Loading branch information
ltackmann committed Apr 1, 2013
1 parent 5abbe23 commit ca6753d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ Powerful logging library with support for multiple appenders, configurable forma

Quick Guide
-----------

1. Add the folowing to your **pubspec.yaml** and run **pub install**
```yaml
dependencies:
log4dart: any
```
2. Add log4dart to some code and run it
```dart
import "package:log4dart/log4dart.dart";
Expand Down
36 changes: 14 additions & 22 deletions lib/src/logger_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,14 @@ class LoggerFactory {
_builder = builder;
}

/**
* Get a [Logger] with the fully qualified name of [type]
*
* WARNING currently this method is very expensive due to lack of features in dart:mirrors
*/
/** Get a [Logger] with the fully qualified name of [type] */
static Logger getLoggerFor(Type type) {
// TODO remove this very expensive lookup method once dart:mirrors allows you to reflect on type
var im = reflect(type);
var typeName = im.reflectee.toString();
var loggerName;
currentMirrorSystem().libraries.forEach((k,v) {
if(!k.startsWith("dart") && v.classes.containsKey(typeName)) {
loggerName = "${k}.${typeName}";
return;
}
});
assert(loggerName != null);
var cm = _getClassMirrorForType(type);
var loggerName = cm.qualifiedName;
return getLogger(loggerName);
}

/**
* Get a [Logger] named [loggerName]
*/
/** Get a [Logger] named [loggerName] */
static Logger getLogger(String loggerName) {
if(!_nameRegex.hasMatch(loggerName)) {
throw new ArgumentError("illegal logger name $loggerName, only letters, underscores and dots allowed");
Expand All @@ -64,15 +49,22 @@ class LoggerFactory {
return logger;
}

/**
* Access and configure the log subsystem
*/
/** Access and configure the log subsystem */
static LoggerConfigMap get config {
if(_configMap == null) {
_configMap = new LoggerConfigMap();
}
return _configMap;
}

// Hack ! Waiting for a true method in dart:mirrors
static ClassMirror _getClassMirrorForType(Type type) {
var name = type.toString();
return currentMirrorSystem().libraries.values
.where((lib) => lib.classes.containsKey(name))
.map((lib) => lib.classes[name])
.first;
}

static LoggerConfigMap _configMap;
static Map<String, Logger> _loggerCache;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: log4dart
version: 1.4.0
version: 1.4.1
author: Lars Tackmann <lars@solvr.io>
description: Powerful logging library with support for multiple appenders, configurable formatting and log tracing.
homepage: https://github.com/ltackmann/log4dart
Expand Down

0 comments on commit ca6753d

Please sign in to comment.