Skip to content

RollingFileLogWriter could be a bit more "open" #471

@Marandil

Description

@Marandil

Currently RollingFileLogWriter itself is declared as open, but all interesting facilities are actually private.
The only "open" fun appears to be log, which makes inheritance redundant (one could use composition instead of inheritance).

I propose the crucial functionality should be made protected instead - at least loggingChannel.

For instance, currently it's not easy to actually provide a custom format for throwable as it bypasses messageStringFormatter (which can actually be configured) and goes straight to bufferLog. With a protected loggingChannel one could derive an object from RollingFileLogWriter, write their own log function and push the formatted message directly with loggingChannel.trySendBlocking(Buffer().apply { writeString(log) }). The last part is currently impossible, because th crucial loggingChannel is private.

The only other solution I can think of is to actually incorporate throwable into message, but this is hacky and not always desired.


This ties into a separate issue that it's not possible to provide custom format for Throwables.
For instance, imagine a structured log format that serializes messages like this:

    val formatter = object : MessageStringFormatter {
        override fun formatMessage(severity: Severity?, tag: Tag?, message: Message): String {
            val timestamp = clock.now().format(DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET)
            return json.encodeToString(
                StructuredLogMessage(
                    timestamp,
                    severity.toString(),
                    tag?.tag,
                    message.message
                )
            )
        }
    }

When this formatter is used with a Throwable, the log writer adds a nicely formatted, structured message, followed by an unstructured stack trace. Instead, it would be nice if we could provide a way to format the Throwable, or get the Throwable as an argument in MessageStringFormatter::formatMessage. The most straightforward workaround would be to manually format the message in log, but this circles back to all the crucial facilities being hidden.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions