Skip to main content

Logger

The Logger utility provides control over logging output from the Mapbox Maps SDK. You can set the log level and customize log handling with a custom callback.

Import

import { Logger } from '@rnmapbox/maps';

Basic Usage

import { Logger } from '@rnmapbox/maps';

// Set log level
Logger.setLogLevel('warning');

// Set custom log callback
Logger.setLogCallback((log) => {
  console.log(`[${log.level}] ${log.tag}: ${log.message}`);
  return true; // Return true to suppress default logging
});

Methods

setLogLevel()

static setLogLevel(level: LogLevel): void
Sets the minimum log level for Mapbox SDK messages.
level
LogLevel
required
The minimum level of logging to display.Valid values:
  • 'error' - Only errors
  • 'warning' - Errors and warnings
  • 'info' - Errors, warnings, and info messages
  • 'debug' - Errors, warnings, info, and debug messages
  • 'verbose' - All log messages
Example:
import { Logger } from '@rnmapbox/maps';

// Show only errors
Logger.setLogLevel('error');

// Show errors and warnings
Logger.setLogLevel('warning');

// Show all logs
Logger.setLogLevel('verbose');

setLogCallback()

static setLogCallback(callback: LogCallback): void
Sets a custom callback function to handle log messages. This allows you to:
  • Redirect logs to your own logging system
  • Filter or modify log messages
  • Suppress default logging
callback
(log: LogObject) => boolean
required
Callback function that receives log objects.Parameters:
  • log: LogObject - The log message object
Returns:
  • boolean - Return true to suppress default logging, false to allow it
LogObject:
{
  level: LogLevel;    // 'error' | 'warning' | 'info' | 'debug' | 'verbose'
  tag: string;        // Log tag (Android) or category
  message: string;    // The log message
}
Example:
import { Logger } from '@rnmapbox/maps';

// Custom logging
Logger.setLogCallback((log) => {
  // Send to custom analytics
  if (log.level === 'error') {
    analytics.logError(log.message);
  }
  
  // Format and log
  console.log(`[Mapbox ${log.level.toUpperCase()}] ${log.message}`);
  
  // Return true to suppress default Mapbox logging
  return true;
});

// Filter specific messages
Logger.setLogCallback((log) => {
  // Suppress specific warnings
  if (log.level === 'warning' && log.message.includes('Canceled')) {
    return true; // Suppress this log
  }
  
  // Allow default logging for everything else
  return false;
});

Types

LogLevel

type LogLevel = 'error' | 'warning' | 'info' | 'debug' | 'verbose';
Log severity levels, from most to least severe.

LogObject

interface LogObject {
  level: LogLevel;
  tag: string;
  message: string;
}
Represents a single log message from the Mapbox SDK.

LogCallback

type LogCallback = (log: LogObject) => boolean;
Callback function type for custom log handling. Return true to suppress default logging.

Examples

import { useEffect } from 'react';
import { Logger } from '@rnmapbox/maps';

function App() {
  useEffect(() => {
    // Set log level based on environment
    if (__DEV__) {
      Logger.setLogLevel('verbose');
    } else {
      Logger.setLogLevel('error');
    }
  }, []);

  return <YourApp />;
}

Default Behavior

The Logger automatically:
  • Starts listening for log events on initialization
  • Filters HTTP request cancellation warnings (common and usually not actionable)
  • Formats logs with [Mapbox level] prefix
  • Routes logs to appropriate console methods (console.error, console.warn, console.log)

Best Practices

  1. Set Log Level Early: Configure logging as early as possible in your app’s lifecycle
  2. Environment-Based Levels: Use verbose logging in development, error-only in production
  3. Custom Callback for Production: Implement custom logging to send errors to monitoring services
  4. Filter Noise: Use the callback to suppress known non-critical warnings
  5. Performance: Avoid expensive operations in the log callback

Common Patterns

Production Error Tracking

if (!__DEV__) {
  Logger.setLogLevel('error');
  Logger.setLogCallback((log) => {
    if (log.level === 'error') {
      Sentry.captureMessage(`Mapbox: ${log.message}`, 'error');
    }
    return true;
  });
}

Development Debugging

if (__DEV__) {
  Logger.setLogLevel('verbose');
  Logger.setLogCallback((log) => {
    console.log(`[${log.level}] ${log.tag || 'Mapbox'}: ${log.message}`);
    return true;
  });
}

Mute Specific Warnings

Logger.setLogCallback((log) => {
  if (log.message.includes('Canceled') && log.tag === 'Mbgl-HttpRequest') {
    return true; // Suppress canceled request warnings
  }
  return false;
});

Build docs developers (and LLMs) love