| .. | ||
| config.js | ||
| example.js | ||
| index.js | ||
| README.md | ||
| scopes.js | ||
Centralized Logging System
A flexible, scope-based logging system for React Native applications that provides consistent log formatting and environment-based filtering.
Features
- Scope-based logging for better organization and filtering
- Environment-based configuration
- Consistent log formatting with timestamps
- Development vs Production configurations
- Multiple log levels (debug, info, warn, error)
- Easy integration with existing codebase
Installation
The logger is already integrated into the project. No additional installation steps required.
Usage
Basic Usage
import { createLogger } from "~/lib/logger";
// Create a logger with scopes
const logger = createLogger({
component: "MyComponent",
feature: "authentication",
});
// Use different log levels
logger.debug("Debug message", { additionalData: "value" });
logger.info("Info message");
logger.warn("Warning message", { threshold: 100 });
logger.error("Error message", new Error("Something went wrong"));
Configuration
Configure the logging system through environment variables:
-
APP_LOG_SCOPES: Comma-separated list of enabled scope namesAPP_LOG_SCOPES=api,auth,networkIf not set, all scopes are enabled
-
APP_LOG_MIN_LEVEL: Minimum log level to displayAPP_LOG_MIN_LEVEL=warn // Only show warn and error logsDefaults to:
- Development: "debug"
- Production: "info"
Log Levels
debug: Detailed information for debugginginfo: General information about application operationwarn: Warning messages for potentially problematic situationserror: Error messages for serious problems
Output Format
[timestamp] [LEVEL] [scope1,scope2] message additional_data
Example:
[2025-02-17T10:39:35.123Z] [INFO] [api,network] Request completed { status: 200 }
Best Practices
-
Use Meaningful Scopes
// Good const logger = createLogger({ component: "UserProfile", feature: "authentication", }); // Not as descriptive const logger = createLogger({ scope: "general", }); -
Include Relevant Context
// Good logger.error("Failed to fetch user data", { userId, error: error.message, statusCode: error.status, }); // Missing important context logger.error("Failed to fetch user data"); -
Use Appropriate Log Levels
debug: Development/debugging informationinfo: Normal application flowwarn: Potential issues that don't block operationerror: Serious problems requiring attention
-
Group Related Logs
// Group related operations under the same scopes const authLogger = createLogger({ module: "auth", feature: "login", }); authLogger.info("Login attempt", { username }); authLogger.debug("Validating credentials"); authLogger.info("Login successful");
Examples
See example.js for more usage examples.
Production Considerations
-
Use
APP_LOG_SCOPESto filter logs in production:APP_LOG_SCOPES=api,auth // Only show logs from api and auth scopes -
Set appropriate minimum log level:
APP_LOG_MIN_LEVEL=warn // Only show warnings and errors -
Consider log volume and performance:
- Avoid excessive debug logs in production
- Include only necessary context in log messages
- Use appropriate log levels to manage output
Contributing
When adding new features or components:
- Create a logger with descriptive scopes
- Use consistent log levels
- Include relevant context in log messages
- Document any new logging patterns