Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use logRecord.getInstant() for timestamp in JitsiLogFormatter logs. #137

Merged
merged 2 commits into from
May 16, 2024
Merged
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
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
Loading