json static method

void json(
  1. dynamic message, {
  2. bool alwaysPrint = false,
  3. bool prettyPrint = false,
})

Log json data message to the console. It will only print if your app's environment is in debug mode. You can override this by setting alwaysPrint = true. Set prettyPrint to true for formatted JSON output with indentation.

Implementation

static void json(
  dynamic message, {
  bool alwaysPrint = false,
  bool prettyPrint = false,
}) {
  // Early exit check before any processing
  bool canPrint = (getEnv(_appDebugKey, defaultValue: true));
  if (!canPrint && !alwaysPrint) return;

  try {
    final encoder = prettyPrint
        ? JsonEncoder.withIndent('  ')
        : const JsonEncoder();
    log(encoder.convert(message));
  } on JsonUnsupportedObjectError catch (e) {
    NyLogger.error('Cannot encode to JSON: ${e.cause}');
  } on Exception catch (e) {
    NyLogger.error(e.toString());
  }
}