DeveloperBreeze

Code Programming Tutorials, Guides & Best Practices

Explore 184+ expertly crafted code tutorials, components, and code examples. Stay productive and build faster with proven implementation strategies and design patterns from DeveloperBreeze.

React Custom Hook for API Requests

Code August 12, 2024
javascript

No preview available for this content.

Python Logging Snippet

Code August 08, 2024
python

This snippet sets up a logger that writes log messages to both the console and a file, with different log levels to capture various types of information.

import logging
import os

# Create a directory for logs if it doesn't exist
if not os.path.exists('logs'):
    os.makedirs('logs')

# Configure the logger
logging.basicConfig(
    level=logging.DEBUG,  # Set the logging level
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('logs/app.log'),  # Log to a file
        logging.StreamHandler()               # Log to the console
    ]
)

# Get the logger instance
logger = logging.getLogger('MyAppLogger')

# Example usage of the logger
def divide_numbers(x, y):
    try:
        logger.debug(f"Attempting to divide {x} by {y}")
        result = x / y
        logger.info(f"Division successful: {result}")
        return result
    except ZeroDivisionError as e:
        logger.error("Division by zero error", exc_info=True)
    except Exception as e:
        logger.exception("An unexpected error occurred")

# Demonstrate logging in action
if __name__ == "__main__":
    divide_numbers(10, 2)  # Normal operation
    divide_numbers(10, 0)  # Division by zero

Fetching Chuck Norris Jokes from API in JavaScript

Code January 26, 2024
javascript

No preview available for this content.

PHP File Upload

Code January 26, 2024
php

No preview available for this content.

Promise-based Execution of Python Code with jsPython

Code January 26, 2024
javascript

  • The first callback (result => { ... }) handles the resolved value, printing out the result.
  • The second callback (error => { ... }) handles any rejected value (errors), printing out the error message.