Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add log to files #39

Open
kelyacf opened this issue Apr 23, 2024 · 0 comments
Open

Add log to files #39

kelyacf opened this issue Apr 23, 2024 · 0 comments

Comments

@kelyacf
Copy link
Collaborator

kelyacf commented Apr 23, 2024

Here's a simple example of Python code that demonstrates how to log messages to a file. This example utilizes Python's built-in logging module, which provides a flexible framework for emitting log messages from Python programs.
python
import logging

Configure logging

Create a logger object

logger = logging.getLogger('example_logger')
logger.setLevel(logging.DEBUG) # Set the minimum level of log to capture

Create file handler which logs even debug messages

fh = logging.FileHandler('example.log')
fh.setLevel(logging.DEBUG)

Create formatter and add it to the handlers

formatter = logging.Formatter('%(asctime)s • %(name)s • %(levelname)s • %(message)s')
fh.setFormatter(formatter)

Add the handlers to the logger

logger.addHandler(fh)

Log some messages

logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')

Explanation:

  1. Import the Logging Module:• The logging module is part of the standard Python library.
  2. Create Logger:• A logger is created by calling getLogger(). You can optionally pass a name, but if you do not, a default one will be provided.
  3. Set Log Level:• The log level specifies the severity of the messages the logger will handle. Lower levels include messages from the higher levels.
  4. Create File Handler:• Creates a handler that writes messages to a file. The file is specified by 'example.log'.
  5. Set Handler Level:• Similar to setting the level for the logger, but this sets the level for this specific handler.
  6. Create and Set Formatter:• Determines the layout of the message in the log file.
  7. Add Handlers to Logger:• Connects the handler (with its specified format) to the logger.
  8. Log Messages:• Send test messages of various severities to the log. Messages below the level set won't be logged.
    This script will create a file named example.log in the same directory where the script is run and will include the logged messages formatted with timestamps, the logger's name, the message's severity level, and the message itself.
    (Geppetto v0.2.1 Source: OpenAI Model gpt-4-turbo)
@kelyacf kelyacf assigned ignaciopenia and unassigned ignaciopenia Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants