|
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); |
|
} |
|
} |
|
} |