Skip to content

Commit

Permalink
refactor log4j and appender
Browse files Browse the repository at this point in the history
  • Loading branch information
clagomess committed Mar 12, 2024
1 parent 3669ab7 commit 07466a4
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 28 deletions.
8 changes: 8 additions & 0 deletions cli/src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Root logger option
log4j.rootLogger=INFO, console

# Direct log messages to stdout
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss} - %m%n
8 changes: 8 additions & 0 deletions core/src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Root logger option
log4j.rootLogger=INFO, console

# Direct log messages to stdout
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss} - %m%n
18 changes: 0 additions & 18 deletions core/src/main/resources/log4j.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import com.github.clagomess.pirilampo.gui.component.ColorChooserComponent;
import com.github.clagomess.pirilampo.gui.component.FileChooserComponent;
import com.github.clagomess.pirilampo.gui.component.RadioButtonGroupComponent;
import com.github.clagomess.pirilampo.gui.util.AppenderUtil;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.util.Arrays;

public class MainForm {
Expand Down Expand Up @@ -85,5 +87,23 @@ public class MainForm {
new RadioButtonGroupComponent.RadioButton<>(LayoutPdfEnum.LANDSCAPE)
));

// progress
public JProgressBar progress = new JProgressBar(){{setStringPainted(true);}};
public JTextArea console = new JTextArea(){{
setLineWrap(true);
setWrapStyleWord(true);
setEditable(false);
setFont(new Font(
"Consolas",
this.getFont().getStyle(),
this.getFont().getSize()
));

AppenderUtil.setOnChange(text -> {
this.setText(text);
this.setCaretPosition(this.getDocument().getLength());
});
}};

public final JButton btnCompile = new JButton("Compile!");
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ public MainUI() {
pTabArtifact.addTab("PDF", pPdf);
add(pTabArtifact, "wrap");

/*
JPanel pProgress = new JPanel(new MigLayout("", "[grow,fill]"));
pProgress.setBorder(BorderFactory.createTitledBorder("Progress"));
pProgress.add(new JProgressBar(), "wrap");
pProgress.add(new JTextArea(), "wrap");
add(pProgress);
*/
pProgress.add(form.progress, "wrap");
pProgress.add(new JScrollPane(
form.console,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
), "height 50, wrap");
add(pProgress, "wrap");

add(form.btnCompile);
getRootPane().setDefaultButton(form.btnCompile);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
package com.github.clagomess.pirilampo.gui.util;

import lombok.Getter;
import lombok.Setter;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;

import java.time.Instant;
import java.time.ZoneId;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class AppenderUtil extends AppenderSkeleton {
@Getter
private static final List<String> events = Collections.synchronizedList(new LinkedList<>());

@Setter
private static OnChangeFI onChange = e -> {};

private final AtomicBoolean canOnChange = new AtomicBoolean(true);

@Override
protected void append(LoggingEvent event) {
/*
if((event.getLevel() == Level.ERROR || event.getLevel() == Level.WARN) && event.getThrowableInformation() != null){
ConsoleBind.setLogData(event.getThrowableInformation().getThrowable().getMessage());
String message;

if((event.getLevel() == Level.ERROR || event.getLevel() == Level.WARN) &&
event.getThrowableInformation() != null
){
message = event.getThrowableInformation().getThrowable().getMessage();
}else{
ConsoleBind.setLogData(event.getRenderedMessage());
message = event.getRenderedMessage();
}

events.add(String.format(
"%s %s: - %s",
event.getLevel(),
Instant.ofEpochMilli(event.getTimeStamp()).atZone(ZoneId.systemDefault()).toLocalDateTime(),
message
));

if (events.size() > 10) events.remove(0);

if (canOnChange.get()) {
canOnChange.set(false);
onChange.change(String.join("\n", events));
canOnChange.set(true);
}
*/
}

@Override
Expand All @@ -22,4 +57,9 @@ public void close() {}
public boolean requiresLayout() {
return false;
}

@FunctionalInterface
public interface OnChangeFI {
void change(String event);
}
}
11 changes: 11 additions & 0 deletions gui/src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Root logger option
log4j.rootLogger=INFO, console, appenderutil

# Direct log messages to stdout
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss} - %m%n

# To Java
log4j.appender.appenderutil=com.github.clagomess.pirilampo.gui.util.AppenderUtil

0 comments on commit 07466a4

Please sign in to comment.