Basics

Logging

Pengantar

Nylo Website v7 menyediakan sistem logging yang komprehensif.

Log hanya dicetak ketika APP_DEBUG=true di file .env Anda, menjaga aplikasi produksi tetap bersih.

import 'package:nylo_framework/nylo_framework.dart';

// Basic logging
printInfo("Hello World");
printDebug("Debug message");
printError("Error occurred");

Level Log

Nylo Website v7 mendukung beberapa level log dengan output berwarna:

Level Method Warna Kasus Penggunaan
Debug printDebug() Cyan Info debugging detail
Info printInfo() Biru Informasi umum
Error printError() Merah Error dan exception
printDebug("Fetching user ID: 123");
printInfo("App initialized");
printError("Network request failed");

Contoh output:

[2025-01-27 10:30:45] [debug] Fetching user ID: 123
[2025-01-27 10:30:45] [info] App initialized
[2025-01-27 10:30:46] [error] Network request failed

Method Log

Logging Dasar

// Class methods
printInfo("Information message");
printDebug("Debug message");
printError("Error message");
printJson({"key": "value"});

Error dengan Stack Trace

Log error dengan stack trace untuk debugging yang lebih baik:

try {
  await someOperation();
} catch (e, stackTrace) {
  printError(e, stackTrace: stackTrace);
}

Paksa Cetak Terlepas dari Mode Debug

Gunakan alwaysPrint: true untuk mencetak bahkan ketika APP_DEBUG=false:

printInfo("Critical info", alwaysPrint: true);
printError("Critical error", alwaysPrint: true);

Tampilkan Log Berikutnya (Override Sekali Pakai)

Cetak satu log ketika APP_DEBUG=false:

// .env: APP_DEBUG=false

printInfo("This won't print");

showNextLog();
printInfo("This will print"); // Prints once

printInfo("This won't print again");

Logging JSON

Nylo Website v7 menyertakan method logging JSON khusus:

Map<String, dynamic> userData = {
  "id": 123,
  "name": "Anthony",
  "email": "anthony@example.com"
};

// Compact JSON
printJson(userData);
// {"id":123,"name":"Anthony","email":"anthony@example.com"}

// Pretty printed JSON
printJson(userData, prettyPrint: true);
// {
//   "id": 123,
//   "name": "Anthony",
//   "email": "anthony@example.com"
// }

Output Berwarna

Nylo Website v7 menggunakan warna ANSI untuk output log dalam mode debug. Setiap level log memiliki warna yang berbeda untuk identifikasi mudah.

Menonaktifkan Warna

// Disable colored output globally
NyLogger.useColors = false;

Warna secara otomatis dinonaktifkan:

  • Dalam mode release
  • Ketika terminal tidak mendukung kode escape ANSI

Listener Log

Nylo Website v7 memungkinkan Anda mendengarkan semua entri log secara real-time:

// Set up a log listener
NyLogger.onLog = (NyLogEntry entry) {
  print("Log: [${entry.type}] ${entry.message}");

  // Send to crash reporting service
  if (entry.type == 'error') {
    CrashReporter.log(entry.message, stackTrace: entry.stackTrace);
  }
};

Properti NyLogEntry

NyLogger.onLog = (NyLogEntry entry) {
  entry.message;    // The log message
  entry.type;       // Log level (debug, info, warning, error, success, verbose)
  entry.dateTime;   // When the log was created
  entry.stackTrace; // Stack trace (for errors)
};

Kasus Penggunaan

  • Kirim error ke layanan pelaporan crash (Sentry, Firebase Crashlytics)
  • Bangun penampil log kustom
  • Simpan log untuk debugging
  • Pantau perilaku aplikasi secara real-time
// Example: Send errors to Sentry
NyLogger.onLog = (entry) {
  if (entry.type == 'error') {
    Sentry.captureMessage(
      entry.message,
      level: SentryLevel.error,
    );
  }
};

Extension Helper

Nylo Website menyediakan method extension yang praktis untuk logging:

dump()

Cetak nilai apa pun ke konsol:

String project = 'Nylo';
project.dump(); // 'Nylo'

List<String> seasons = ['Spring', 'Summer', 'Fall', 'Winter'];
seasons.dump(); // ['Spring', 'Summer', 'Fall', 'Winter']

int age = 25;
age.dump(); // 25

// Function syntax
dump("Hello World");

dd() - Dump and Die

Cetak nilai dan langsung keluar (berguna untuk debugging):

String code = 'Dart';
code.dd(); // Prints 'Dart' and stops execution

// Function syntax
dd("Debug point reached");

Konfigurasi

Variabel Environment

Kontrol perilaku logging di file .env Anda:

# Enable/disable all logging
APP_DEBUG=true

DateTime di Log

Nylo Website dapat menyertakan timestamp di output log. Konfigurasikan ini di setup Nylo Anda:

// In your boot provider
Nylo.instance.showDateTimeInLogs(true);

Output dengan timestamp:

[2025-01-27 10:30:45] [info] User logged in

Output tanpa timestamp:

[info] User logged in

Praktik Terbaik

  1. Gunakan level log yang sesuai - Jangan log semua sebagai error
  2. Hapus log verbose di produksi - Tetap APP_DEBUG=false di produksi
  3. Sertakan konteks - Log data yang relevan untuk debugging
  4. Gunakan logging terstruktur - NyLogger.json() untuk data kompleks
  5. Siapkan monitoring error - Gunakan NyLogger.onLog untuk menangkap error
// Good logging practice
NyLogger.info("User ${user.id} logged in from ${device.platform}");
NyLogger.error("API request failed", stackTrace: stackTrace, alwaysPrint: true);
NyLogger.json(response.data, prettyPrint: true);