encode method

String encode(
  1. Log log
)

Encodes log as a single JSON line (no trailing newline).

Implementation

String encode(Log log) {
  final levelTheme = theme[log.level];

  return jsonEncode(<String, Object?>{
    'num': log.num,
    'level': log.level,
    'levelName': log.levelName,
    'time': log.time.toUtc().toIso8601String(),
    if (log.path.isNotEmpty) 'path': log.path,
    if (log.traceIds.isNotEmpty)
      'traceIds': [for (final id in log.traceIds) id.toString()],
    'message': levelTheme.formatMessage(levelTheme.formatValue(log.message)),
    if (log.tags.isNotEmpty) 'tags': [...log.tags],
    if (log.hasData)
      'data': switch (dataFormat) {
        FileLogDataFormat.text => Loggable.objectToString(
            log.data,
            theme: levelTheme,
            config: config,
          ),
        FileLogDataFormat.json =>
          Loggable.objectToJson(log.data, config: jsonConfig),
      },
    if (log.error != null) 'error': log.error.toString(),
    if (log.stackTrace != null) 'stackTrace': log.stackTrace.toString(),
  });
}