java.lang.Object
net.i2p.util.LogManager
All Implemented Interfaces:
Flushable

public class LogManagerextends Object implements Flushable
Manages the logging system, loading (and reloading) the configuration file, coordinating the log limits, and storing the set of log records pending. This also fires off a LogWriter thread that pulls pending records off and writes them where appropriate.

As of 0.9.41, this class may be overridden via I2PAppContext.setLogManager(LogManager).

  • Field Details

    • CONFIG_LOCATION_PROP

      public static final String CONFIG_LOCATION_PROP
      Property name for the logger config file location.
      See Also:
    • FILENAME_OVERRIDE_PROP

      public static final String FILENAME_OVERRIDE_PROP
      Property name to override the log filename.
      See Also:
    • CONFIG_LOCATION_DEFAULT

      public static final String CONFIG_LOCATION_DEFAULT
      Default logger config file name.
      See Also:
    • DATE

      public static final char DATE
      These define the characters in the format line of the config file.
      See Also:
    • CLASS

      public static final char CLASS
      Format placeholder that expands to the logging class name.
      See Also:
    • THREAD

      public static final char THREAD
      Format placeholder that expands to the logging thread name.
      See Also:
    • PRIORITY

      public static final char PRIORITY
      Format placeholder that expands to the log record's priority.
      See Also:
    • MESSAGE

      public static final char MESSAGE
      Format placeholder that expands to the log record's message.
      See Also:
    • PROP_FORMAT

      public static final String PROP_FORMAT
      Property name for the log record format string.
      See Also:
    • PROP_DATEFORMAT

      public static final String PROP_DATEFORMAT
      Property name for the date format pattern.
      See Also:
    • PROP_FILENAME

      public static final String PROP_FILENAME
      Property name for the log file name pattern.
      See Also:
    • PROP_FILESIZE

      public static final String PROP_FILESIZE
      Property name for the log file size limit.
      See Also:
    • PROP_ROTATIONLIMIT

      public static final String PROP_ROTATIONLIMIT
      Property name for the log rotation limit.
      See Also:
    • PROP_DISPLAYONSCREEN

      public static final String PROP_DISPLAYONSCREEN
      Property name to enable or disable on-screen display.
      See Also:
    • PROP_CONSOLEBUFFERSIZE

      public static final String PROP_CONSOLEBUFFERSIZE
      Property name for the console buffer size.
      See Also:
    • PROP_DISPLAYONSCREENLEVEL

      public static final String PROP_DISPLAYONSCREENLEVEL
      Property name for the minimum log level displayed on screen.
      See Also:
    • PROP_DEFAULTLEVEL

      public static final String PROP_DEFAULTLEVEL
      Property name for the default log level.
      See Also:
    • PROP_GROUP_READABLE

      public static final String PROP_GROUP_READABLE
      Property name for group-readable (660) log files. When enabled, the current log and rotated .gz files are readable/writable by the group the router runs under, so sysadmins who are members of that group can inspect the logs. The files are never world-readable.
      See Also:
    • PROP_LOG_BUFFER_SIZE

      private static final String PROP_LOG_BUFFER_SIZE
      Config key for the log buffer size.
      See Also:
    • PROP_DROP

      private static final String PROP_DROP
      Config key to drop messages on buffer overflow.
      See Also:
    • PROP_DUP

      private static final String PROP_DUP
      Config key for dropping duplicate messages.
      See Also:
    • PROP_FLUSH

      private static final String PROP_FLUSH
      Config key for the log flush interval.
      See Also:
    • PROP_GZIP

      private static final String PROP_GZIP
      Config key for log gzipping.
      See Also:
    • PROP_MIN_GZIP_SIZE

      private static final String PROP_MIN_GZIP_SIZE
      Config key for the minimum log size to gzip.
      See Also:
    • PROP_RECORD_PREFIX

      public static final String PROP_RECORD_PREFIX
      Prefix for per-logger level override properties.
      See Also:
    • DEFAULT_FORMAT

      public static final String DEFAULT_FORMAT
      Default log record format string.
      See Also:
    • DEFAULT_DATEFORMAT

      public static final String DEFAULT_DATEFORMAT
      Date pattern used when none is configured.
      See Also:
    • DEFAULT_FILENAME

      public static final String DEFAULT_FILENAME
      Default log file name pattern.
      See Also:
    • DEFAULT_FILESIZE

      public static final String DEFAULT_FILESIZE
      Default log file size limit as a string.
      See Also:
    • DEFAULT_DISPLAYONSCREEN

      public static final boolean DEFAULT_DISPLAYONSCREEN
      Default setting for on-screen log display.
      See Also:
    • DEFAULT_CONSOLEBUFFERSIZE

      public static final int DEFAULT_CONSOLEBUFFERSIZE
      Default number of entries in the console buffer.
      See Also:
    • DEFAULT_ROTATIONLIMIT

      public static final String DEFAULT_ROTATIONLIMIT
      Default log rotation limit.
      See Also:
    • DEFAULT_DEFAULTLEVEL

      public static final String DEFAULT_DEFAULTLEVEL
      Default log level string.
      See Also:
    • DEFAULT_ONSCREENLEVEL

      public static final String DEFAULT_ONSCREENLEVEL
      Default on-screen log level string.
      See Also:
    • MIN_FILESIZE_LIMIT

      private static final int MIN_FILESIZE_LIMIT
      Smallest log file size allowed.
      See Also:
    • DEFAULT_GZIP

      private static final boolean DEFAULT_GZIP
      Default for compressing the rotated logs.
      See Also:
    • DEFAULT_MIN_GZIP_SIZE

      private static final int DEFAULT_MIN_GZIP_SIZE
      Default smallest log size to compress.
      See Also:
    • _context

      private final I2PAppContext _context
      The app context.
    • _log

      private final Log _log
      Logger for the log manager itself.
    • _configLastRead

      private long _configLastRead
      When was the config file last read (or -1 if never).
    • _locationFile

      private File _locationFile
      The config file.
    • MAX_BUFFER

      private static final int MAX_BUFFER
      Max LogRecords to buffer in memory before we start blocking.
      See Also:
    • _records

      private final LinkedBlockingQueue<LogRecord> _records
      Ordered list of LogRecord elements that have not been written out yet.
    • _limits

      private final Set<LogLimit> _limits
      List of explicit overrides of log levels (LogLimit objects).
    • _logs

      private final ConcurrentHashMap<Object,Log> _logs
      String (scope) or Log.LogScope to Log object.
    • _writer

      private volatile LogWriter _writer
      Who clears and writes our records. Volatile so the writer started here is safely published to the callers that read the field without holding this object's monitor.
    • _shutdown

      private volatile boolean _shutdown
      True once the manager has shut down.
    • _defaultLimit

      private volatile int _defaultLimit
      Default log level for logs that aren't explicitly controlled through a LogLimit in _limits.
    • _format

      private volatile char[] _format
      Log record format string.
    • _dateFormat

      private SimpleDateFormat _dateFormat
      Date format instance.
    • _dateFormatPattern

      private String _dateFormatPattern
      Date format string (for the SimpleDateFormat instance).
    • _baseLogfilename

      private String _baseLogfilename
      Log filename pattern.
    • _fileSize

      private volatile int _fileSize
      Max bytes in the logfile before rotation.
    • _rotationLimit

      private int _rotationLimit
      Max rotated logs.
    • _onScreenLimit

      private volatile int _onScreenLimit
      Minimum log level to be displayed on stdout.
    • _displayOnScreen

      private volatile boolean _displayOnScreen
      Whether or not we even want to display anything on stdout.
    • _consoleBufferSize

      private int _consoleBufferSize
      How many records we want to buffer in the "recent logs" list.
    • _consoleBuffer

      private final LogConsoleBuffer _consoleBuffer
      The actual "recent logs" list.
    • _logBufferSize

      private int _logBufferSize
      Size of the in memory log buffer.
    • _dropOnOverflow

      private volatile boolean _dropOnOverflow
      True to drop records when the buffer is full.
    • _dropDuplicates

      private boolean _dropDuplicates
      True to drop repeated records.
    • _droppedRecords

      private final AtomicLong _droppedRecords
      Records dropped since startup.
    • _flushInterval

      private int _flushInterval
      Flush interval of the writers, in seconds.
    • _gzip

      private volatile boolean _gzip
      True to compress the rotated logs.
    • _minGzipSize

      private long _minGzipSize
      Smallest rotated log that gets compressed.
    • _alreadyNoticedMissingConfig

      private boolean _alreadyNoticedMissingConfig
      True once the missing config file was logged.
  • Constructor Details

    • LogManager

      public LogManager(I2PAppContext context)
      Create the log manager for the given context.
      Parameters:
      context - the I2P app context
  • Method Details

    • startLogWriter

      private void startLogWriter()
      Start the log writer thread, if it isn't running already. Synchronized so only one writer is started; the field itself is volatile because the threads that use the writer do not hold this monitor.
    • getLog

      public final Log getLog(Class<?> cls)
      Log for the given class.
      Parameters:
      cls - the class to get the log for
      Returns:
      the shared Log instance scoped to that class
    • getLog

      public final Log getLog(String name)
      Log with the given name.
      Parameters:
      name - the scope suffix distinguishing this log from other logs in the same manager
      Returns:
      the shared Log instance scoped to that name
    • getLog

      public final Log getLog(Class<?> cls, String name)
      Log for the given class and name, creating it if necessary.
      Parameters:
      cls - the class (may be null)
      name - the scope suffix, or null to scope the log by class name alone
      Returns:
      the shared Log instance for that class and scope, created if it does not exist yet
    • getLogs

      public List<Log> getLogs()
      Return all currently cached Log instances. Now used by ConfigLogingHelper.
      Returns:
      list of all Log objects tracked by this manager
    • addLog

      void addLog(Log log)
      If the log already exists, its priority is set here but cannot be changed later, as it becomes an "orphan" not tracked by the manager.
      Parameters:
      log - the Log to add and track
    • getBuffer

      public LogConsoleBuffer getBuffer()
      Return the circular buffer holding recent log records for console display.
      Returns:
      the console buffer
    • displayOnScreen

      public boolean displayOnScreen()
      Whether log messages should be printed to stdout.
      Returns:
      whether to display log on screen
    • getDisplayOnScreenLevel

      public int getDisplayOnScreenLevel()
      Minimum log level for messages printed to stdout.
      Returns:
      the minimum level for on-screen display
    • getConsoleBufferSize

      public int getConsoleBufferSize()
      Maximum number of entries held in the console buffer.
      Returns:
      the console buffer size
    • setConfig

      public final void setConfig(String filename)
      Configure the logger from the given config file, reloading the configuration.
      Parameters:
      filename - the config file path
    • currentFile

      public String currentFile()
      File may not exist or have old logs in it if not opened yet.
      Returns:
      non-null
    • addRecord

      void addRecord(LogRecord record)
      Used by Log to add records to the queue. This is generally nonblocking and unsyncrhonized but may block when under massive logging load as a way of throttling logging threads.
      Parameters:
      record - the log record to enqueue
    • rereadConfig

      void rereadConfig()
      Called periodically by the LogWriter thread to reload the config file. Do not log here, deadlock of LogWriter.
    • shouldDropDuplicates

      boolean shouldDropDuplicates()
      Whether duplicate log records should be dropped.
      Returns:
      true if duplicate records are dropped
    • loadConfig

      private void loadConfig()
      Load and parse the logger configuration file if it has changed. Do not log here, deadlock of LogWriter via rereadConfig().
    • parseConfig

      private void parseConfig(Properties config)
      Parse all configuration properties and set internal state accordingly. Do not log here, deadlock of LogWriter via rereadConfig().
      Parameters:
      config - the configuration properties to parse
    • parseLimits

      private void parseLimits(Properties config)
      Do not log here, deadlock of LogWriter via rereadConfig().
      Parameters:
      config - the properties to read the log limits from, may be null
    • parseLimits

      private void parseLimits(Properties config, String recordPrefix)
      Do not log here, deadlock of LogWriter via rereadConfig().
      Parameters:
      config - the properties to read the log limits from, may be null
      recordPrefix - required key prefix, stripped before matching; null for any key
    • setLimits

      public void setLimits(Properties limits)
      Update the existing limit overrides.
      Parameters:
      limits - mapping of prefix to log level string (not the log #)
    • setDateFormat

      public boolean setDateFormat(String format)
      Update the date format Do not log here, deadlock of LogWriter via rereadConfig().
      Parameters:
      format - null or empty string means use default format for the locale (with a SHORT date and a MEDIUM time - see DateFormat)
      Returns:
      true if the format was updated, false if it was invalid
    • setFileSize

      public void setFileSize(int numBytes)
      Update the log file size limit.
      Parameters:
      numBytes - the requested limit in bytes, raised to the 16 KiB minimum; zero or negative is ignored
    • getDefaultLimit

      public String getDefaultLimit()
      Default limit for logs without explicit overrides, as a text string.
      Returns:
      the default log limit as a string
    • setDefaultLimit

      public void setDefaultLimit(String lim)
      Configure the default log limit from a text string.
      Parameters:
      lim - the default log limit string
    • getLimits

      public Properties getLimits()
      Return a mapping of the explicit overrides - path prefix to (text formatted) limit.
      Returns:
      the explicit overrides, keyed by path prefix, with text-formatted limits
    • getFileSize

      public static int getFileSize(String size)
      Determine how many bytes are in the given formatted string (5m, 60g, 100k, etc) Size may be k, m, or g. A trailing b is ignored. Upper-case is allowed. Spaces between the number and letter is are allowed. The number may be in floating point. 16K min, 2 GB max (returns int).
      Parameters:
      size - the formatted size to parse, such as "5m", "60g" or "100k"
      Returns:
      the file size
    • updateLimits

      private void updateLimits()
      Do not log here, deadlock of LogWriter via rereadConfig().
    • updateLimit

      private void updateLimit(Log log)
      Do not log here, deadlock of LogWriter via rereadConfig().
      Parameters:
      log - the log to apply the longest matching limit to
    • getLimits

      private List<LogLimit> getLimits(Log log)
      Do not log here, deadlock of LogWriter via rereadConfig().
      Parameters:
      log - the log to match the configured limits against
      Returns:
      null if no matches
    • getBaseLogfilename

      public String getBaseLogfilename()
      Base log filename pattern.
      Returns:
      the pattern with the substitutions unexpanded
    • setBaseLogfilename

      public void setBaseLogfilename(String filenamePattern)
      Configure the base log filename pattern.
      Parameters:
      filenamePattern - the base log filename pattern
    • getFileSize

      public int getFileSize()
      Maximum log file size in bytes before rotation.
      Returns:
      the maximum log file size in bytes
    • getRotationLimit

      public int getRotationLimit()
      Number of rotated log files to keep.
      Returns:
      the rotation limit (number of log files to keep)
    • shouldGzip

      public boolean shouldGzip()
      Check if gzip compression should be used for rotated log files.
      Returns:
      true if rotated log files should be gzip compressed
    • setGzip

      public void setGzip(boolean yes)
      Enable or disable gzip compression for rotated log files.
      Parameters:
      yes - true to enable gzip compression
    • getMinGzipSize

      long getMinGzipSize()
      Minimum size for gzip compression.
      Returns:
      the minimum size in bytes, or Long.MAX_VALUE if gzip is disabled
    • saveConfig

      public boolean saveConfig()
      Save the current logging configuration to disk.
      Returns:
      true if the configuration was saved successfully
    • createConfig

      private Properties createConfig()
      Writes the default logging configuration.
      Returns:
      the properties written
    • getQueue

      Queue<LogRecord> getQueue()
      Zero-copy. For the LogWriter.
      Returns:
      the live bounded record queue the LogWriter drains, handed over without copying
    • getFormat

      public char[] getFormat()
      Log format characters.
      Returns:
      the format, as a character array
    • setFormat

      public void setFormat(char[] fmt)
      Configure the log format characters.
      Parameters:
      fmt - the log format characters
    • getDateFormat

      public SimpleDateFormat getDateFormat()
      Any usage of returned formatter must be synchronized!
      Returns:
      the date format
    • getDateFormatPattern

      public String getDateFormatPattern()
      Date format pattern string.
      Returns:
      the pattern used for the log dates
    • flush

      public void flush()
      Flush any pending records to disk. Blocking up to 250 ms.
      Specified by:
      flush in interface Flushable
    • shutdown

      public void shutdown()
      Shut down the log manager, flushing and stopping the writer.
    • getContext

      I2PAppContext getContext()
      Convenience method for LogRecordFormatter.
      Returns:
      the router context this LogManager was created in