Skip to content

Commit

Permalink
Use logRecord.getInstant() for timestamp in JitsiLogFormatter logs. (#…
Browse files Browse the repository at this point in the history
…137)

Also use DateTimeFormatter to format the timestamp, and some other warning cleanups.
  • Loading branch information
JonathanLennox authored May 16, 2024
1 parent e0b9606 commit 83984af
Showing 1 changed file with 13 additions and 34 deletions.
47 changes: 13 additions & 34 deletions src/main/java/org/jitsi/utils/logging2/JitsiLogFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package org.jitsi.utils.logging2;

import java.io.*;
import java.text.*;
import java.util.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.logging.*;
import java.util.logging.Formatter;

public class JitsiLogFormatter extends Formatter
{
Expand All @@ -38,17 +37,7 @@ public class JitsiLogFormatter extends Formatter
/**
* Line separator used by current platform
*/
private static String lineSeparator = System.getProperty("line.separator");

/**
* Two digit <tt>DecimalFormat</tt> instance, used to format datetime
*/
private static DecimalFormat twoDigFmt = new DecimalFormat("00");

/**
* Three digit <tt>DecimalFormat</tt> instance, used to format datetime
*/
private static DecimalFormat threeDigFmt = new DecimalFormat("000");
private static final String lineSeparator = System.lineSeparator();

/**
* The application name used to generate this log
Expand All @@ -60,6 +49,12 @@ public class JitsiLogFormatter extends Formatter
*/
private static boolean timestampDisabled = false;

/**
* The formatter to use for timestamps.
*/
private static final DateTimeFormatter timestampFormatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS ");

/**
* The default constructor for <tt>JitsiLogFormatter</tt> which loads
* program name property from logging.properties file, if it exists
Expand All @@ -77,7 +72,7 @@ public JitsiLogFormatter()
@Override
public synchronized String format(LogRecord record)
{
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();

if (programName != null)
{
Expand All @@ -89,23 +84,7 @@ public synchronized String format(LogRecord record)

if (!timestampDisabled)
{
//current time
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minutes = cal.get(Calendar.MINUTE);
int seconds = cal.get(Calendar.SECOND);
int millis = cal.get(Calendar.MILLISECOND);

sb.append(year).append('-');
sb.append(twoDigFmt.format(month)).append('-');
sb.append(twoDigFmt.format(day)).append(' ');
sb.append(twoDigFmt.format(hour)).append(':');
sb.append(twoDigFmt.format(minutes)).append(':');
sb.append(twoDigFmt.format(seconds)).append('.');
sb.append(threeDigFmt.format(millis)).append(' ');
sb.append(timestampFormatter.format(ZonedDateTime.ofInstant(record.getInstant(), ZoneId.systemDefault())));
}

//log level
Expand Down Expand Up @@ -151,9 +130,9 @@ public synchronized String format(LogRecord record)
PrintWriter pw = new PrintWriter(sw);
record.getThrown().printStackTrace(pw);
pw.close();
sb.append(sw.toString());
sb.append(sw);
}
catch (RuntimeException ex)
catch (RuntimeException ignored)
{
}
}
Expand Down

0 comments on commit 83984af

Please sign in to comment.