Created
September 25, 2010 20:46
-
-
Save squeedee/597294 to your computer and use it in GitHub Desktop.
CouchDB Flex Logging Target
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.visfleet.util { | |
import com.adobe.serialization.json.JSONDecoder; | |
import com.adobe.serialization.json.JSONEncoder; | |
import com.visfleetOld.iv.DateUtil; | |
import com.visfleetOld.iv.Invalidator; | |
import flash.events.Event; | |
import flash.net.URLLoader; | |
import flash.net.URLRequest; | |
import flash.net.URLRequestHeader; | |
import flash.net.URLRequestMethod; | |
import mx.logging.AbstractTarget; | |
import mx.logging.LogEvent; | |
import mx.utils.StringUtil; | |
public class CouchTarget extends AbstractTarget { | |
private var loader:URLLoader; | |
private var committing:Boolean = false; | |
private var pending:Boolean = false; | |
private var _dbName:String; | |
private var _documentName:String; | |
private var _log:Object; | |
private var _dbHost:String; | |
private static const DB_DEFAULT_NAME:String = "logging"; | |
private static const DEFAULT_HOST:String = "http://127.0.0.1:5984"; | |
public function CouchTarget() { | |
loader = new URLLoader(); | |
loader.addEventListener(Event.COMPLETE, handleComplete, false, 0, true); | |
reset(); | |
} | |
override public function logEvent(event:LogEvent):void { | |
var now:Date = new Date(); | |
var message:String = event.message; | |
var line:* = { | |
level: LogEvent.getLevelString(event.level), | |
date: now.toDateString(), | |
time: now.toTimeString() | |
}; | |
if (event.message.match(/^\{/)) | |
line.message = JSONDecoder(message).getValue(); | |
else | |
line.message = message; | |
_log.lines[now.time] = line; | |
Invalidator.doLater(commit); | |
} | |
private function handleComplete(event:Event):void { | |
committing = false; | |
_log._rev = new JSONDecoder(loader.data).getValue().rev; | |
if (pending) { | |
pending = false; | |
Invalidator.doLater(commit); | |
} | |
} | |
private function commit():void { | |
if (committing) { | |
pending = true; | |
return; | |
} | |
committing = true; | |
var request:URLRequest = new URLRequest(dbHost + "/" + dbName + "/" + documentName); | |
request.method = URLRequestMethod.POST; | |
addHeaders(request, {'X-HTTP-METHOD-OVERRIDE': 'PUT'}); | |
request.data = new JSONEncoder(_log).getString(); | |
request.contentType = "application/json"; | |
loader.load(request); | |
} | |
// Pinched this from http://github.com/dima/restfulx_framework/blob/master/framework/src/org/restfulx/services/http/XMLHTTPServiceProvider.as | |
protected function addHeaders(request:URLRequest, headers:Object):void { | |
if (request.requestHeaders == null) request.requestHeaders = []; | |
for (var key:String in headers) { | |
request.requestHeaders.push(new URLRequestHeader(key, headers[key])); | |
} | |
} | |
private function reset():void { | |
_log = { | |
lines: {} | |
} | |
} | |
public function get dbHost():String { | |
return _dbHost || DEFAULT_HOST; | |
} | |
public function set dbHost(value:String):void { | |
if (_dbHost == value) | |
return; | |
reset(); | |
_dbHost = value; | |
} | |
public function get dbName():String { | |
return _dbName || DB_DEFAULT_NAME; | |
} | |
public function set dbName(value:String):void { | |
if (_dbName == value) | |
return; | |
reset(); | |
_dbName = value; | |
} | |
public function get documentName():String { | |
return _documentName || getDefaultDocument(); | |
} | |
public function set documentName(value:String):void { | |
if (_documentName == value) | |
return; | |
reset(); | |
_documentName = value; | |
} | |
private function getDefaultDocument():String { | |
_documentName = StringUtil.substitute("session_{0}", escape(DateUtil.toW3CDTF(new Date(), false, false))); | |
return _documentName; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment