diff --git a/src/ufront/log/BasicMessageFormatter.hx b/src/ufront/log/BasicMessageFormatter.hx new file mode 100644 index 0000000..0cd9ec1 --- /dev/null +++ b/src/ufront/log/BasicMessageFormatter.hx @@ -0,0 +1,13 @@ +package ufront.log; + +class BasicMessageFormatter implements UFMessageFormatter { + var formatter:Message->String; + + public function new(formatter:Message->String) { + this.formatter = formatter; + } + + public function format(m:Message):String { + return formatter(m); + } +} \ No newline at end of file diff --git a/src/ufront/log/BrowserConsoleLogger.hx b/src/ufront/log/BrowserConsoleLogger.hx index d6c72b9..a6fa2a7 100644 --- a/src/ufront/log/BrowserConsoleLogger.hx +++ b/src/ufront/log/BrowserConsoleLogger.hx @@ -26,19 +26,23 @@ __Client Behaviour__ If running client-side, the message will be traced to the console directly using Javascript. **/ class BrowserConsoleLogger implements UFLogHandler { - public function new() {} + var messageFormatter:UFMessageFormatter; + + public function new(?messageFormatter:UFMessageFormatter) { + this.messageFormatter = messageFormatter == null ? new MessageFormatter() : messageFormatter; + } public function log( ctx:HttpContext, appMessages:Array ) { #if server if( ctx.response.contentType=="text/html" && !ctx.response.isRedirect() ) { var results = []; for( msg in ctx.messages ) - results.push( formatMessage(msg) ); + results.push( messageFormatter.format(msg) ); #if debug if ( appMessages!=null) { for( msg in appMessages ) - results.push( formatMessage(msg) ); + results.push( messageFormatter.format(msg) ); } #end @@ -59,24 +63,6 @@ class BrowserConsoleLogger implements UFLogHandler { return SurpriseTools.success(); } - /** - A helper to create a `console.log`, `console.info`, `console.warn` or `console.error` Javascript snippet. - When executed by the client, this snippet will send the given message to the client's browser console. - **/ - static function formatMessage( m:Message ):String { - var type = switch (m.type) { - case MTrace: "log"; - case MLog: "info"; - case MWarning: "warn"; - case MError: "error"; - } - var extras = - if ( m.pos!=null && m.pos.customParams!=null ) ", "+m.pos.customParams.join(", ") - else ""; - var msg = '${m.pos.className}.${m.pos.methodName}(${m.pos.lineNumber}): ${m.msg}$extras'; - return 'console.${type}(decodeURIComponent("${StringTools.urlEncode(msg)}"))'; - } - #if client /** A client-side helper to print the current message to the `js.html.Console`. @@ -106,3 +92,25 @@ class BrowserConsoleLogger implements UFLogHandler { } #end } + +/** +A helper to create a `console.log`, `console.info`, `console.warn` or `console.error` Javascript snippet. +When executed by the client, this snippet will send the given message to the client's browser console. +**/ +private class MessageFormatter implements UFMessageFormatter { + public function new(){} + + public function format(m:Message):String { + var type = switch (m.type) { + case MTrace: "log"; + case MLog: "info"; + case MWarning: "warn"; + case MError: "error"; + } + var extras = + if ( m.pos!=null && m.pos.customParams!=null ) ", "+m.pos.customParams.join(", ") + else ""; + var msg = '${m.pos.className}.${m.pos.methodName}(${m.pos.lineNumber}): ${m.msg}$extras'; + return 'console.${type}(decodeURIComponent("${StringTools.urlEncode(msg)}"))'; + } +} diff --git a/src/ufront/log/FileLogger.hx b/src/ufront/log/FileLogger.hx index 0684f76..918033c 100644 --- a/src/ufront/log/FileLogger.hx +++ b/src/ufront/log/FileLogger.hx @@ -48,8 +48,11 @@ class FileLogger implements UFLogHandler implements UFInitRequired { /** The relative or absolute path to the log file. **/ public var path(default,null):String; - public function new( path:String ) { + var messageFormatter:UFMessageFormatter; + + public function new( path:String, ?messageFormatter:UFMessageFormatter ) { this.path = path; + this.messageFormatter = messageFormatter == null ? new MessageFormatter() : messageFormatter; } public function init( app:HttpApplication ) { @@ -76,9 +79,9 @@ class FileLogger implements UFLogHandler implements UFInitRequired { var content = '${Date.now()} [${req.httpMethod}] [${req.uri}] from [$userDetails], response: [${res.status} ${res.contentType}]\n'; for( msg in context.messages ) - content += '\t${format(msg)}\n'; + content += '\t${messageFormatter.format(msg)}\n'; if ( appMessages!=null) for( msg in appMessages ) - content += '\t${format(msg)}\n'; + content += '\t${messageFormatter.format(msg)}\n'; #if sys FileSystem.createDirectory( logFile.directory() ); @@ -92,9 +95,13 @@ class FileLogger implements UFLogHandler implements UFInitRequired { return SurpriseTools.asBadSurprise( HttpError.notImplemented() ); #end } +} - /** Format a message in a suitable way for a text-only log file. **/ - public static function format( msg:Message ):String { +/** Format a message in a suitable way for a text-only log file. **/ +private class MessageFormatter implements UFMessageFormatter { + public function new(){} + + public function format( msg:Message ):String { var msgStr = Std.string( msg.msg ); var text = REMOVENL.replace( msgStr, '\\n' ); var type = Type.enumConstructor( msg.type ).substr( 1 ); diff --git a/src/ufront/log/RemotingLogger.hx b/src/ufront/log/RemotingLogger.hx index 29dcddb..c1aecd2 100644 --- a/src/ufront/log/RemotingLogger.hx +++ b/src/ufront/log/RemotingLogger.hx @@ -17,19 +17,24 @@ If `-debug` is defined, any application level messages (those from "trace" rathe If the `HttpRequest` does not contain the `X-Ufront-Remoting` header, or the `HttpResponse.contentType` is not "application/x-haxe-remoting", the traces will not be displayed. **/ class RemotingLogger implements UFLogHandler { - public function new() {} + var messageFormatter:UFMessageFormatter; + + public function new(?messageFormatter:UFMessageFormatter) { + this.messageFormatter = messageFormatter == null ? new MessageFormatter() : messageFormatter; + } + public function log( httpContext:HttpContext, appMessages:Array ) { if( httpContext.request.clientHeaders.exists("X-Ufront-Remoting") && httpContext.response.contentType=="application/x-haxe-remoting" ) { var results = []; for( msg in httpContext.messages ) - results.push( formatMessage(msg) ); + results.push( messageFormatter.format(msg) ); #if debug if ( appMessages!=null) { for( msg in appMessages ) - results.push( formatMessage(msg) ); + results.push( messageFormatter.format(msg) ); } #end @@ -40,8 +45,12 @@ class RemotingLogger implements UFLogHandler { return SurpriseTools.success(); } +} - static function formatMessage( m:Message ):String { +private class MessageFormatter implements UFMessageFormatter { + public function new() {} + + public function format( m:Message ):String { // Make sure everything is converted to a String before we serialize it. m.msg = ''+m.msg; if ( m.pos.customParams != null) { diff --git a/src/ufront/log/ServerConsoleLogger.hx b/src/ufront/log/ServerConsoleLogger.hx index 565c04b..3427a95 100644 --- a/src/ufront/log/ServerConsoleLogger.hx +++ b/src/ufront/log/ServerConsoleLogger.hx @@ -18,7 +18,12 @@ This will use a different method on each platform where it makes sense: This will flush the messages (traces, logs, warnings and errors) from the current context to the appropriate server log. **/ class ServerConsoleLogger implements UFLogHandler { - public function new() {} + var messageFormatter:UFMessageFormatter; + + public function new(?messageFormatter:UFMessageFormatter) { + this.messageFormatter = messageFormatter == null ? new MessageFormatter() : messageFormatter; + } + public function log( ctx:HttpContext, appMessages:Array ) { var messages = []; @@ -34,11 +39,11 @@ class ServerConsoleLogger implements UFLogHandler { messages.push( requestLog ); for( msg in ctx.messages ) - messages.push( formatMsg(msg) ); + messages.push( messageFormatter.format(msg) ); if ( appMessages!=null) { for( msg in appMessages ) - messages.push( formatMsg(msg) ); + messages.push( messageFormatter.format(msg) ); } writeLog( messages.join("\n ") ); @@ -46,14 +51,6 @@ class ServerConsoleLogger implements UFLogHandler { return SurpriseTools.success(); } - static function formatMsg( m:Message ):String { - var extras = - if ( m.pos!=null && m.pos.customParams!=null ) ", "+m.pos.customParams.join(", ") - else ""; - var type = Type.enumConstructor( m.type ).substr( 1 ); - return '$type: ${m.pos.className}.${m.pos.methodName}(${m.pos.lineNumber}): ${m.msg}$extras'; - } - static function writeLog( message:String, ?type:MessageType=null ):Void { #if neko neko.Web.logMessage( message ); @@ -66,3 +63,15 @@ class ServerConsoleLogger implements UFLogHandler { #end } } + +private class MessageFormatter implements UFMessageFormatter { + public function new() {} + + public function format(m:Message):String { + var extras = + if ( m.pos!=null && m.pos.customParams!=null ) ", "+m.pos.customParams.join(", ") + else ""; + var type = Type.enumConstructor( m.type ).substr( 1 ); + return '$type: ${m.pos.className}.${m.pos.methodName}(${m.pos.lineNumber}): ${m.msg}$extras'; + } +} \ No newline at end of file diff --git a/src/ufront/log/UFMessageFormatter.hx b/src/ufront/log/UFMessageFormatter.hx new file mode 100644 index 0000000..e888720 --- /dev/null +++ b/src/ufront/log/UFMessageFormatter.hx @@ -0,0 +1,5 @@ +package ufront.log; + +interface UFMessageFormatter { + function format(message:Message):String; +} \ No newline at end of file