Class DittoLogger
Singleton with static methods to customize Ditto's logging behavior.
Ditto uses the persistence directory of the Ditto instance that was most recently
created to store a limited amount of logs. Ditto may continue writing logs to a persistence
directory even after the associated Ditto instance is deallocated.
If this is a concern, consider either disabling logging by setting
setEnabled(boolean) to false, or instantiating a new Ditto instance.
After either of these actions, it is safe to remove the persistence directory.
Please refer to exportToFileBlocking(String) for further details on locally collected logs.
-
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull DittoULongexportToFileBlocking(@NonNull String path) Exports collected logs to a compressed and JSON-encoded file on the local file system.static @NonNull DittoLogLevelReturns the minimum log level at which logs will be logged, providedisEnabled()istrue.static booleanChecks whether logging is enabled.static voidsetCustomLogCallback(@NonNull DittoLogCallback logCallback) Registers a callback for custom handling of log events.static voidsetEnabled(boolean newValue) Sets whether logging is enabled.static voidsetLogFile(@Nullable String logFile) Registers a file path where logs will be written, in addition to console output.static voidsetMinimumLogLevel(@NonNull DittoLogLevel logLevel) Sets the minimum log level at which logs will be logged, providedisEnabled()istrue.static voidUnregisters the currently registered custom log callback.
-
Method Details
-
isEnabled
public static boolean isEnabled()Checks whether logging is enabled.
Logs exported through
exportToFileBlocking(String)are not affected by this setting and will also include logs emitted whileisEnabled()isfalse.- Returns:
trueif logging is enabled,falseotherwise.
-
setEnabled
public static void setEnabled(boolean newValue) Sets whether logging is enabled.
Logs exported through
exportToFileBlocking(String)are not affected by this setting and will also include logs emitted whileisEnabled()isfalse.- Parameters:
newValue-trueto enable logging,falseto disable.
-
getMinimumLogLevel
Returns the minimum log level at which logs will be logged, provided
isEnabled()istrue.For example, if this is set to
DittoLogLevel.WARNING, then only logs that are logged with theDittoLogLevel.WARNINGorDittoLogLevel.ERRORlog levels will be shown.Logs exported through
exportToFileBlocking(String)are not affected by this setting and include all logs atDittoLogLevel.DEBUGand above.- Returns:
- the minimum
DittoLogLevel.
-
setMinimumLogLevel
Sets the minimum log level at which logs will be logged, provided
isEnabled()istrue.For example, if this is set to
DittoLogLevel.WARNING, then only logs that are logged with theDittoLogLevel.WARNINGorDittoLogLevel.ERRORlog levels will be shown.Logs exported through
exportToFileBlocking(String)are not affected by this setting and include all logs atDittoLogLevel.DEBUGand above.- Parameters:
logLevel- the minimumDittoLogLevel.
-
exportToFileBlocking
Exports collected logs to a compressed and JSON-encoded file on the local file system.
DittoLogger locally collects a limited amount of logs at
DittoLogLevel.DEBUGlevel and above, periodically discarding old logs. This internal logger is always enabled and works independently of theisEnabled()setting and the configuredgetMinimumLogLevel(). Its logs can be requested and downloaded from any peer that is active in a Ditto app using the portal's device dashboard. This method provides an alternative way of accessing those logs by exporting them to the local file system.The logs will be written as a gzip compressed file at the path specified by the
pathparameter. When uncompressed, the file contains one JSON value per line with the oldest entry on the first line (JSON lines format).By default, Ditto limits the amount of logs it retains on disk to 15 MB and a maximum age of 15 days. Older logs are periodically discarded once one of these limits is reached.
This method currently only exports logs from the most recently created
Dittoinstance, even when multiple instances are running in the same process.For more granular exception handling check the
DittoException.IoExceptionReasonsubtypes.- Parameters:
path- the path of the file to write the logs to. The file must not already exist, and the containing directory must exist. It is recommended for the path to have a.jsonl.gzfile extension but Ditto won't enforce it.- Returns:
- the number of bytes written to disk.
- Throws:
DittoException- when the file cannot be written to disk. Prevent this by ensuring that no file exists at the provided path, all parent directories exist, sufficient permissions are granted, and that the disk is not full.- See Also:
-
setLogFile
Registers a file path where logs will be written, in addition to console output.
Whenever Ditto emits a log, it will be written to both the console and the specified file.
- Parameters:
logFile- the file path where logs should be written, ornullto unregister the current logging file. If not null, the file path must be within an already existing directory.
-
setCustomLogCallback
Registers a callback for custom handling of log events.
This provides a fully customizable way of handling log events from the logger, in addition to logging to the console and to a file (if configured).
- Parameters:
logCallback- theDittoLogCallbackto be called each time a log statement is issued by Ditto (after filtering by log level). Must not benull.
-
unsetCustomLogCallback
public static void unsetCustomLogCallback()Unregisters the currently registered custom log callback.
After calling this method, the custom callback (if any was set) will no longer be invoked.
-