Skip to content

Commit

Permalink
User timer instead of dummy while true sleep loops
Browse files Browse the repository at this point in the history
  • Loading branch information
StrongestNumber9 committed Jun 16, 2022
1 parent 238f5ed commit 65c0e7d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 71 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/teragrep/jla_02/JavaUtilLoggingLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.teragrep.jla_04.*;

import java.io.*;
import java.util.TimerTask;
import java.util.concurrent.TimeoutException;
import java.util.logging.*;

public class JavaUtilLoggingLogger {
public class JavaUtilLoggingLogger extends TimerTask {
static {
}
static Logger logger = Logger.getLogger(JavaUtilLoggingLogger.class.getName());
Expand All @@ -33,7 +34,8 @@ public JavaUtilLoggingLogger() {
logger.addHandler(handler);
}

public void Log() {
@Override
public void run() {
try {
logger.info("JavaUtilLoggingLogger info says hi");
logger.warning("JavaUtilLoggingLogger warning says hi");
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/teragrep/jla_02/Log4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class Log4j {
import java.util.TimerTask;

public class Log4j extends TimerTask {

private static Logger logger;

Expand All @@ -12,7 +14,8 @@ public Log4j() {
logger = Logger.getLogger(Log4j.class);
}

public void Log() {
@Override
public void run() {
try {
logger.info("Log4j info says hi!");
logger.warn("Log4j warn says hi!");
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/teragrep/jla_02/Log4j2.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory;
import org.apache.logging.log4j.LogManager;

public class Log4j2 {
import java.util.TimerTask;

public class Log4j2 extends TimerTask {
private static Logger logger;
private static Marker LOG_AUDIT;
private static Marker LOG_DAILY;
Expand All @@ -20,7 +22,8 @@ public Log4j2() {
LOG_METRIC = MarkerManager.getMarker("METRIC");
}

public void Log() {
@Override
public void run() {
try {
logger.info(LOG_AUDIT, "Log4j2 info audit says hi!");
logger.info(LOG_DAILY, "Log4j2 info daily says hi!");
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/teragrep/jla_02/Logback.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import org.slf4j.MarkerFactory;
import ch.qos.logback.classic.util.ContextInitializer;

public class Logback {
import java.util.TimerTask;

public class Logback extends TimerTask {
private static Logger logger;
private static Marker LOG_AUDIT = MarkerFactory.getMarker("AUDIT");
private static Marker LOG_DAILY = MarkerFactory.getMarker("DAILY");
Expand All @@ -22,7 +24,8 @@ public Logback() {

}

public void Log() {
@Override
public void run() {
try {
logger.info(LOG_AUDIT, "Logback-audit says hi.");
logger.info(LOG_DAILY, "Logback-daily says hi.");
Expand Down
78 changes: 17 additions & 61 deletions src/main/java/com/teragrep/jla_02/Main.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
package com.teragrep.jla_02;

import java.text.SimpleDateFormat;
import java.util.Timer;

public class Main {
public static void main(String[] args) {
final int sleep_duration = (System.getenv("SLEEP_DURATION") == null) ? 5 : Integer.parseInt(System.getenv("SLEEP_DURATION"));
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final int sleep_duration = ((System.getenv("SLEEP_DURATION") == null) ? 5 : Integer.parseInt(System.getenv("SLEEP_DURATION")))*1000;

Timer timer = new Timer();

System.out.println("Sending events every " + sleep_duration + " milliseconds");

Thread Log4j2 = new Thread(
new Runnable(){
public void run(){
System.out.println("Starting Log4j2");
Log4j2 log4j2 = new Log4j2();
while(true) {
try {
log4j2.Log();
Thread.sleep(sleep_duration * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
}
timer.scheduleAtFixedRate(new Log4j2(), 0, sleep_duration);
}
}
);
Expand All @@ -30,16 +25,7 @@ public void run(){
new Runnable(){
public void run(){
System.out.println("Starting Log4j");
Log4j log4j = new Log4j();
while(true) {
try {
log4j.Log();
Thread.sleep(sleep_duration * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
}
timer.scheduleAtFixedRate(new Log4j(), 0, sleep_duration);
}
}
);
Expand All @@ -49,16 +35,7 @@ public void run(){
new Runnable(){
public void run(){
System.out.println("Starting JavaUtilLoggingLogger");
JavaUtilLoggingLogger javautillogginglogger = new JavaUtilLoggingLogger();
while(true) {
try {
javautillogginglogger.Log();
Thread.sleep(sleep_duration * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
}
timer.scheduleAtFixedRate(new JavaUtilLoggingLogger(), 0, sleep_duration);
}
}
);
Expand All @@ -68,17 +45,7 @@ public void run(){
new Runnable(){
public void run(){
System.out.println("Starting Logback");
Logback logback = new Logback();
while(true) {
try {
logback.Log();
Thread.sleep(sleep_duration * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
}

timer.scheduleAtFixedRate(new Logback(), 0, sleep_duration);
}
}
);
Expand All @@ -88,29 +55,18 @@ public void run(){
new Runnable() {
public void run() {
System.out.println("Starting Tinylog");
Tinylog tinylog = new Tinylog();
while (true) {
try {
tinylog.Log();
Thread.sleep(sleep_duration * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
}
// timer.scheduleAtFixedRate(new Tinylog(), 0, sleep_duration);
}
}
);
// Tinylog.start(); // WIP

while(true) {
try {
Thread.sleep(1000000);
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
try {
Thread.currentThread().join();
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
7 changes: 5 additions & 2 deletions src/main/java/com/teragrep/jla_02/Tinylog.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import org.pmw.tinylog.Logger;

public class Tinylog {
public void Log() {
import java.util.TimerTask;

public class Tinylog extends TimerTask {
@Override
public void run() {
try {
Logger.info("Tinylog info says hi!");
Logger.warn("Tinylog warn says hi!");
Expand Down

0 comments on commit 65c0e7d

Please sign in to comment.