Skip to content

Commit

Permalink
LDEV-5206 add LogExecutionLog
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed Dec 19, 2024
1 parent e63a63d commit 8cfbaee
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 10 deletions.
13 changes: 3 additions & 10 deletions core/src/main/java/lucee/runtime/engine/ConsoleExecutionLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import java.io.PrintWriter;
import java.util.Map;

import lucee.commons.io.log.Log;
import lucee.commons.io.log.LogUtil;
import lucee.commons.io.res.util.ResourceSnippet;
import lucee.commons.io.res.util.ResourceSnippetsMap;
import lucee.runtime.PageContext;
Expand All @@ -44,9 +42,8 @@ protected void _init(PageContext pc, Map<String, String> arguments) {
if (pw == null) {
// stream type
String type = arguments.get("stream-type");
if (type == null) return;
if (type.trim().equalsIgnoreCase("error")) pw = new PrintWriter(System.err);
else if (type.trim().equalsIgnoreCase("out")) pw = new PrintWriter(System.out);
else pw = new PrintWriter(System.out);
}
}

Expand All @@ -62,12 +59,8 @@ protected void _log(int startPos, int endPos, long startTime, long endTime) {
} else {
log += positions(startPos, endPos) + " > " + timeLongToString(diff);
}
if (pw != null){
pw.print(log + "\n");
pw.flush();
} else {
LogUtil.log(pc, Log.LEVEL_TRACE, Controler.class.getName(), log);
}
pw.print(log + "\n");
pw.flush();
}

@Override
Expand Down
76 changes: 76 additions & 0 deletions core/src/main/java/lucee/runtime/engine/LogExecutionLog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
*
* Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
**/
package lucee.runtime.engine;

import java.io.PrintWriter;
import java.util.Map;

import lucee.commons.io.log.Log;
import lucee.commons.io.log.LogUtil;
import lucee.commons.io.res.util.ResourceSnippet;
import lucee.commons.io.res.util.ResourceSnippetsMap;
import lucee.commons.lang.StringUtil;
import lucee.runtime.PageContext;
import lucee.runtime.PageSource;
import lucee.runtime.PageContextImpl;
import lucee.runtime.op.Caster;

public class LogExecutionLog extends ExecutionLogSupport {

private PageContext pc;
private ResourceSnippetsMap snippetsMap = new ResourceSnippetsMap(1024, 128);
private boolean snippet = false;
private int logLevel;
private String logName;

@Override
protected void _init(PageContext pc, Map<String, String> arguments) {
this.pc = pc;
if (Caster.toBooleanValue(arguments.get("snippet"), false)) snippet = true;
String type = arguments.get("log-level");
logLevel = LogUtil.toLevel(type, Log.LEVEL_TRACE);
logName = StringUtil.toString(arguments.get("log-name"), Controler.class.getName());
}

@Override
protected void _log(int startPos, int endPos, long startTime, long endTime) {
PageSource ps = pc.getCurrentPageSource(null);
if (ps == null) return;
long diff = endTime - startTime;
String log = pc.getId() + ":" + ps.getDisplayPath() + ":";
if (snippet) {
ResourceSnippet snippet = snippetsMap.getSnippet(ps, startPos, endPos, ((PageContextImpl) pc).getResourceCharset().name());
log += positions(snippet.getEndLine(), snippet.getEndLine()) + " > " + timeLongToString(diff) + " [" + snippet.getContent() + "]";
} else {
log += positions(startPos, endPos) + " > " + timeLongToString(diff);
}
LogUtil.log(pc, logLevel, Controler.class.getName(), log);
}

@Override
protected void _release() {
snippetsMap = null;
}

private static String positions(int startPos, int endPos) {
if (startPos == endPos) return startPos + "";
return startPos + ":" + endPos;
}

}

0 comments on commit 8cfbaee

Please sign in to comment.