/

Basic Logging

ByteHide Logger provides six logging levels with simple, intuitive methods. Each level serves different purposes and accepts various parameters for flexible logging.

Logging Methods Overview

MethodPurpose
Log.trace(message)Most detailed diagnostic information
Log.debug(message)Detailed diagnostic information
Log.info(message)General information messages
Log.warn(message)Warning messages
Log.error(message)Error messages
Log.critical(message)Critical errors

Basic Logging Methods

Trace Level

Use for the most detailed diagnostic information:

Java
Log.trace("Entering method processOrder");
Log.trace("Processing item 1 of 10");
Log.trace("Database connection established");

Debug Level

Use for detailed diagnostic information during development:

Java
Log.debug("User authentication started");
Log.debug("Cache miss for key: user_123");
Log.debug("API response received in 250ms");

Info Level

Use for general information about application flow:

Java
Log.info("Application started successfully");
Log.info("User logged in");
Log.info("Order processed successfully");

Advanced Logging Methods

Warn Level

Use for potential issues that don't stop execution:

Java
Log.warn("API rate limit approaching");

try {
    // Some operation
} catch (Exception e) {
    Log.warn("Non-critical operation failed, using fallback");
}

Error Level

Use for errors that affect functionality:

Java
try {
    processOrder(orderId);
} catch (Exception e) {
    Log.error("Failed to process order", e);
}

Critical Level

Use for critical errors that may cause application termination:

Java
try {
    initializeDatabase();
} catch (Exception e) {
    Log.critical("Failed to initialize database - application cannot continue");
    System.exit(1);
}

Direct API with Tags and Context

The direct API allows passing tags and context inline:

Java
import java.util.Arrays;
import java.util.Map;

// With tags
Log.info("User logged in", Arrays.asList("auth", "login"), null);

// With context
Map<String, Object> context = Map.of("userId", "123", "username", "johndoe");
Log.info("User action", null, context);

// With both
Log.info("Order created",
    Arrays.asList("order", "payment"),
    Map.of("orderId", "ORD-123", "amount", 99.99)
);

Fluent Syntax

ByteHide Logger supports fluent syntax for combining multiple features:

Java
// Combine context and tags
Log.withContext("orderId", "ORD-001")
   .withContext("customerId", "CUST-123")
   .withTags("orders", "critical")
   .error("Order processing failed");

// With metadata
Log.withMetadata("executionTimeMs", 1250)
   .withContext("sqlQuery", "SELECT * FROM orders")
   .withTags("database", "performance")
   .warn("Slow database query detected");

Learn More About Fluent Syntax

For comprehensive examples and advanced patterns, see Fluent Syntax.

Exception Logging

Java
try {
    processPayment(paymentRequest);
} catch (PaymentException e) {
    Log.error("Payment processing failed", e);
}

// With fluent API
try {
    riskyOperation();
} catch (Exception e) {
    Log.withTags("error", "critical")
       .withException(e)
       .error("Critical operation failed");
}

Practical Examples

Service Layer Logging

Java
public class OrderService {
    public Order createOrder(CreateOrderRequest request) {
        Log.info("Creating new order");
        Log.debug("Validating order request");
        
        try {
            Order order = new Order(request);
            Log.trace("Order object created");
            
            repository.save(order);
            Log.info("Order created successfully");
            
            return order;
        } catch (ValidationException e) {
            Log.warn("Order validation failed");
            throw e;
        } catch (Exception e) {
            Log.withContext("customerId", request.getCustomerId())
               .withException(e)
               .error("Failed to create order");
            throw e;
        }
    }
}

Best Practices

When to Use Each Level

  • Trace: Method entry/exit, loop iterations, detailed flow
  • Debug: Variable values, cache hits/misses, intermediate results
  • Info: Application milestones, user actions, business events
  • Warn: Recoverable errors, deprecated usage, performance issues
  • Error: Exceptions, failed operations, data inconsistencies
  • Critical: System failures, security breaches, unrecoverable errors

Next Steps

Previous
Disable Logging