Access keysTop, Summary, Constructors,
Instance properties,
Class methods,
Instance methodsLogSupport
| Kind of class: |
class |
| Inherits from: |
BasicClass
|
| Known subclasses: |
|
| Version: |
2.0 |
| Author: |
Martin Heidegger |
| Classpath: |
org.as2lib.env.log.LogSupport |
| File last modified: |
Wednesday, 12 October 2005, 17:29:28 |
/*
* Copyright the original author or authors.
*
* Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (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.mozilla.org/MPL/MPL-1.1.html
*
* 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.
*/
import org.as2lib.core.BasicClass;
import org.as2lib.env.log.Logger;
import org.as2lib.env.reflect.ReflectUtil;
import org.as2lib.env.log.LogManager;
/**
* {@code LogSupport} can be used to easily gain access to loggers.
*
* <p>You may extend this class to be able to access the logger appropriate to
* your specific class with the getter property {@link logger} or the {@code getLogger}
* method.
*
* <p>Example:
* <code>
* class MyClass extends LogSupport {
*
* public function test() {
* logger.info("hi");
* }
*
* }
* </code>
*
* @author Martin Heidegger
* @version 2.0
*/
class org.as2lib.env.log.LogSupport extends BasicClass {
/**
* Returns the logger for the given {@code scope}.
*
* @param scope the scope to return a logger for
* @return the logger corresponding to the given {@code scope}
*/
public static function getLoggerByScope(scope):Logger {
if (typeof scope == "function") {
return getLoggerByClass(scope);
} else {
return getLoggerByInstance(scope);
}
}
/**
* Return the logger for the given {@code instance}.
*
* @param instance the instance to return a logger for
* @return the logger corresponding to the given {@code instance}
*/
public static function getLoggerByInstance(instance):Logger {
var p = instance.__proto__;
if (p.__as2lib__logger !== null && p.__as2lib__logger === p.__proto__.__as2lib__logger) {
return storeLoggerInPrototype(p, LogManager.getLogger(
ReflectUtil.getTypeNameForInstance(instance)));
}
return p.__as2lib__logger;
}
/**
* Returns the logger for the given {@code clazz}.
*
* @param clazz the clazz to return a logger for
* @return the logger corresponding to the given {@code clazz}
*/
public static function getLoggerByClass(clazz:Function):Logger {
var p = clazz["prototype"];
if (p.__as2lib__logger !== null && p.__as2lib__logger === p.__proto__.__as2lib__logger) {
return storeLoggerInPrototype(p, LogManager.getLogger(
ReflectUtil.getTypeNameForType(clazz)));
}
return p.__as2lib__logger;
}
/**
* Stores the given {@code logger} in the given {@code prototype}.
*
* @param prototype the prototype to store the given {@code logger} in
* @param logger the logger to store in the given {@code prototype}
* @return the given {@code logger}
*/
private static function storeLoggerInPrototype(prototype, logger:Logger):Logger {
prototype.__as2lib__logger = logger;
if (!prototype.__as2lib__logger) {
prototype.__as2lib__logger = null;
}
return logger;
}
/** The logger for your sub-class. */
public var logger:Logger;
/**
* Constructs a new {@code LogSupport} instance.
*/
public function LogSupport(Void) {
// creates property within constructor because of Macromedia scope bug
addProperty("logger", getLogger, null);
}
/**
* Returns the logger for this instance.
*
* @return the logger corresponding to this instance
*/
public function getLogger(Void):Logger {
return getLoggerByInstance(this);
}
}
LogSupport can be used to easily gain access to loggers.
You may extend this class to be able to access the logger appropriate to
your specific class with the getter property logger or the getLogger
method.
Example:
class MyClass extends LogSupport {
public function test() {
logger.info("hi");
}
}
Constructor
LogSupport
function LogSupport (
Void)
Constructs a new LogSupport instance.
Instance properties
logger
The logger for your sub-class.
Class methods
getLoggerByClass
static function getLoggerByClass (
Returns the logger for the given clazz.
Parameters:
clazz:
the clazz to return a logger for
Returns:the logger corresponding to the given clazz
getLoggerByInstance
static function getLoggerByInstance (
Return the logger for the given instance.
Parameters:
instance:
the instance to return a logger for
Returns:the logger corresponding to the given instance
getLoggerByScope
static function getLoggerByScope (
Returns the logger for the given scope.
Parameters:
scope:
the scope to return a logger for
Returns:the logger corresponding to the given scope
storeLoggerInPrototype
static private function storeLoggerInPrototype (
Stores the given logger in the given prototype.
Parameters:
prototype:
the prototype to store the given logger in
logger :
the logger to store in the given prototype
Instance methods
getLogger
Returns the logger for this instance.
Returns:the logger corresponding to this instance