Giter Club home page Giter Club logo

log_decorator's Introduction

log_decorator

Managing logs effectively is crucial for debugging and monitoring applications. Python provides the logging module for this purpose, and you can use decorators to create a custom log management system. Here's an example of a Python decorator for logging:

import logging

# Configure the logging module
logging.basicConfig(filename='app.log', level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# Decorator function for logging
def log_function_call(func):
    def wrapper(*args, **kwargs):
        # Log the function call
        logging.info(f'Calling function: {func.__name__}')
        
        # Call the original function and capture its return value
        result = func(*args, **kwargs)
        
        # Log the result, if applicable
        logging.info(f'Function result: {result}')
        
        return result
    
    return wrapper

# Usage of the decorator
@log_function_call
def add(a, b):
    return a + b

@log_function_call
def subtract(a, b):
    return a - b

# Test the decorated functions
result1 = add(5, 3)
result2 = subtract(10, 4)

In this example:

We configure the logging module to log messages to a file named 'app.log' with a minimum logging level of INFO. We also define a custom log message format.

We create a decorator function log_function_call. This decorator logs the function name before calling the original function and logs the result afterward.

The wrapper function within the decorator captures any arguments and keyword arguments passed to the original function, calls the original function, logs the function name and result, and returns the result.

We apply the @log_function_call decorator to the add and subtract functions.

When we call add(5, 3) and subtract(10, 4), the decorator logs information about these function calls and their results.

This is a basic example of using a Python decorator for logging function calls. You can customize it further to suit your specific logging requirements, such as adding timestamps, log levels, or handling exceptions.

log_decorator's People

Contributors

jandaghianamin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.