Details
-
Improvement
-
Resolution: Unresolved
-
Major
-
None
-
None
-
None
Description
We needed to change our log file names to use GMT-based naming patterns (LBCORE-85 addresses this potential improvement). The only way to do this is to access package-scoped fields in TimeBasedRollingPolicy and FileNamePattern.
The options we considered were:
1. Copying and tweaking all of TimeBasedRollingPolicy and FileNamePattern and related classes. We'd then have to stay on top of and merge in any bug fixes or improvements in new versions of logback.
2. Write our subclasses in the ch.qos.logback.core.rolling and ch.qos.logback.core.rolling.helper packages, which required un-sealing our jar.
The request is to either change the fields from package-scoped to protected; or add protected getters and setters for accessing them. These are the fields we use:
- TimeBasedRollingPolicy
- activeFileNamePattern
- FileNamePattern
- headTokenConverter
- DateTokenConverter
- sdf
Here's our source code:
public class TimeBasedRollingPolicyWithTimeZone<E> extends TimeBasedRollingPolicy<E> { private String m_timeZone = "GMT"; public void setTimeZone(String timeZone) { m_timeZone = timeZone; } public String getTimeZone() { return m_timeZone; } @Override public void start() { super.start(); activeFileNamePattern = new FileNamePatternWithTimeZone(activeFileNamePattern, TimeZone.getTimeZone(getTimeZone())); } }
public class FileNamePatternWithTimeZone extends FileNamePattern { public FileNamePatternWithTimeZone(FileNamePattern noZonePattern, TimeZone timeZone) { super(noZonePattern.getPattern(), noZonePattern.getContext()); Converter<Object> conv = headTokenConverter; while (conv != null) { if (conv instanceof DateTokenConverter) { ((DateTokenConverter) conv).sdf.setTimeZone(timeZone); } conv = conv.getNext(); } } }