Skip to content

Instantly share code, notes, and snippets.

@judoole
Created August 15, 2012 08:04
Show Gist options
  • Save judoole/3357534 to your computer and use it in GitHub Desktop.
Save judoole/3357534 to your computer and use it in GitHub Desktop.
Logging to Java-server Flex with AMF
package com.github.judoole.log {
import flash.utils.getQualifiedClassName;
import mx.logging.ILogger;
import mx.logging.Log;
public function getLogger(c:Class):ILogger {
var className:String = getQualifiedClassName(c).replace("::", ".")
return Log.getLogger(className);
}
}

Usage

Create

<log:ServerLogTarget channelset="{myChannelset}" destination="remoteObjectLogger" level="{LogEventLevel.WARN}"/>

Filesomething.as

....
private static const LOG:ILogger = getLogger(FileSomething);

public function logSomething(something:String) : void{
       LOG.warn(something);
}
@Service
@RemotingDestination
public class RemoteObjectLogger {
private static final String PRE_FLEX = "[FLEX] ";
protected final Log logger = LogFactory.getLog(getClass());
@RemotingInclude
public void log(String level, String message) {
if ("INFO".equals(level)) logger.info(PRE_FLEX + message);
else if ("DEBUG".equals(level)) logger.debug(PRE_FLEX + message);
else if ("WARN".equals(level)) logger.warn(PRE_FLEX + message);
else if ("ERROR".equals(level)) logger.error(PRE_FLEX + message);
else if ("FATAL".equals(level)) logger.fatal(PRE_FLEX + message);
else logger.info(PRE_FLEX + "[" + level + "] - " + message);
}
}
package com.github.judoole.log {
import mx.logging.AbstractTarget;
import mx.logging.ILogger;
import mx.logging.LogEvent;
import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;
import mx.messaging.channels.SecureAMFChannel;
import mx.rpc.events.FaultEvent;
import mx.rpc.remoting.RemoteObject;
public class ServerLogTarget extends AbstractTarget {
public var url:String;
public var channelset:ChannelSet;
public var destination:String;
private var _remote:RemoteObject;
private function get remote():RemoteObject {
if (_remote) return _remote;
_remote = createRemote(url, destination, channelset);
return _remote;
}
override public function logEvent(event:LogEvent):void {
// Take care to NOT include mx.*, as code in std lib issues log calls,
// which will call this target, which will send a message to server
// (through rpc operation), which will log to this again, ... until StackOverflowException
var category:String = ILogger(event.target).category;
if (category.indexOf("mx.") != -1) {
return;
}
if (remote) {
remote.log(LogEvent.getLevelString(event.level), event.message);
}
}
/** Creates a RemoteObject using SecureAMFChannel if https */
private function createRemote(url:String, destination:String, channelSet:ChannelSet):RemoteObject {
if (url == null && channelset == null) throw new Error("[ServerLogTarget] URK or Channelset to Serverlogger cannot be empty");
if (destination == null) throw new Error("[ServerLogTarget] Destination to Serverlogger cannot be empty");
channelSet = channelSet != null ? channelSet : createChannelSet(url);
var remoteService:RemoteObject = new RemoteObject(destination);
remoteService.channelSet = channelSet;
remoteService.addEventListener(FaultEvent.FAULT, onRemoteFault);
return remoteService;
}
private function createChannelSet(url:String):ChannelSet {
var channelSet:ChannelSet = new ChannelSet();
channelSet.addChannel(isHttps(url) ? new SecureAMFChannel(null, url) : new AMFChannel(null, url));
return channelSet;
}
private function isHttps(url:String):Boolean {
return url.indexOf("https") != -1;
}
/** Default FaultHandler. Just traces the message. */
private function onRemoteFault(evt:FaultEvent):void {
trace("Could not log to server: " + evt.fault.message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment