Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/ufront/log/BasicMessageFormatter.hx
Original file line number Diff line number Diff line change
@@ -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);
}
}
50 changes: 29 additions & 21 deletions src/ufront/log/BrowserConsoleLogger.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message> ) {
#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

Expand All @@ -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`.
Expand Down Expand Up @@ -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)}"))';
}
}
17 changes: 12 additions & 5 deletions src/ufront/log/FileLogger.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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() );
Expand All @@ -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 );
Expand Down
17 changes: 13 additions & 4 deletions src/ufront/log/RemotingLogger.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message> ) {

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

Expand All @@ -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) {
Expand Down
31 changes: 20 additions & 11 deletions src/ufront/log/ServerConsoleLogger.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message> ) {
var messages = [];
Expand All @@ -34,26 +39,18 @@ 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 ") );

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 );
Expand All @@ -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';
}
}
5 changes: 5 additions & 0 deletions src/ufront/log/UFMessageFormatter.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ufront.log;

interface UFMessageFormatter {
function format(message:Message):String;
}