Create a log file

71,253

Solution 1

You can use the logging module to accomplish this.

At the very easiest level, it will be set up like this:

logging.basicConfig(filename="logfilename.log", level=logging.INFO)

There are a number of different levels that you can use to write to the file, such as:

logging.info('your text goes here')
logging.error('your text goes here')
logging.debug('your text goes here')

You can use these lines anywhere that you want to log to the file. If you want to replace the console printing with logging all together, just replace the print lines with logging.info(.......)

For more info on the topic, such as more configurable options (such as timestamps), check the docs (python 3): https://docs.python.org/3/library/logging.html

Solution 2

Logging in python is very efficient and easy to use. You just have to define a python module for logging using python internal logging module. You can define as many logger as you want. You can also configure it to print the output to a console as well write to a file. Apart from this you can define a rotating file handler which will do a log rotation as well which helps in log rotation automation. Below is the snippet to directly define and call the logger in any python module.

import sys
import logging
from logging.config import dictConfig

logging_config = dict(
    version=1,
    formatters={
        'verbose': {
            'format': ("[%(asctime)s] %(levelname)s "
                       "[%(name)s:%(lineno)s] %(message)s"),
            'datefmt': "%d/%b/%Y %H:%M:%S",
        },
        'simple': {
            'format': '%(levelname)s %(message)s',
        },
    },
    handlers={
        'api-logger': {'class': 'logging.handlers.RotatingFileHandler',
                           'formatter': 'verbose',
                           'level': logging.DEBUG,
                           'filename': 'logs/api.log',
                           'maxBytes': 52428800,
                           'backupCount': 7},
        'batch-process-logger': {'class': 'logging.handlers.RotatingFileHandler',
                             'formatter': 'verbose',
                             'level': logging.DEBUG,
                             'filename': 'logs/batch.log',
                             'maxBytes': 52428800,
                             'backupCount': 7},
        'console': {
            'class': 'logging.StreamHandler',
            'level': 'DEBUG',
            'formatter': 'simple',
            'stream': sys.stdout,
        },
    },
    loggers={
        'api_logger': {
            'handlers': ['api-logger', 'console'],
            'level': logging.DEBUG
        },
        'batch_process_logger': {
            'handlers': ['batch-process-logger', 'console'],
            'level': logging.DEBUG
        }
    }
)

dictConfig(logging_config)

api_logger = logging.getLogger('api_logger')
batch_process_logger = logging.getLogger('batch_process_logger')

once you have defined this file (say logger_settings.py), you can import it anywhere and use.

from logger_settings import api_logger

api_logger.info('hello world')

Hope this help. Thanks

Solution 3

To create the log file we can use logging package in python. Code to create log file -

import logging
LOG_FILENAME = "logfile.log"
for handler in logging.root.handlers[:]:
    logging.root.removeHandler(handler)
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)    
logging.info('Forecasting Job Started...')
logging.debug('abc method started...')

And if you want to create the log file timestamp than we can accomplish that using datetime package. code to create log file with timestamp -

from datetime import datetime
LOG_FILENAME = datetime.now().strftime('D:/log/logfile_%H_%M_%S_%d_%m_%Y.log')

Solution 4

The simple way is to use the function above. This code avoid the duplicate logs and to run more than one log file;

import logging

def LOG_insert(file, format, text, level):
    infoLog = logging.FileHandler(file)
    infoLog.setFormatter(format)
    logger = logging.getLogger(file)
    logger.setLevel(level)
    
    if not logger.handlers:
        logger.addHandler(infoLog)
        if (level == logging.INFO):
            logger.info(text)
        if (level == logging.ERROR):
            logger.error(text)
        if (level == logging.WARNING):
            logger.warning(text)
    
    infoLog.close()
    logger.removeHandler(infoLog)
    
    return

formatLOG = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
LOG_insert("file.log", formatLOG , "New log", logging.INFO)

Solution 5

there are many way to write output into the '.log' file

Logging is a means of tracking events it happen when some file runs. Is also indicate that certain events have occurred.

import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.INFO)
logging.debug('This is debug message')
logging.info('This is information message')
logging.warning('This is warning message')
logging.error('This is warning message')

another method to use to reduce all that thing sinple what ever you print to the console that all will be saved to the ''log'' file

python abc.py > abc.log

by using this method you can write everything to the log file

Share:
71,253
antwilson1720
Author by

antwilson1720

Updated on February 18, 2022

Comments

  • antwilson1720
    antwilson1720 about 2 years

    I'm looking to create a log file for my discord bot which is built with python.

    I have a few set of commands which output the console through the print command, I have added a date and time to the print outputs so it can be tracked when the bot is running. However, is it easy to make it save the print outs to a file as well? That way I can make a log file to track different days and what was called for.

    Console Output: Screenshot_1.png

    Example of a print command in my code:

    async def coin(ctx):

    author = ctx.message.author
    choice = random.randint(1,2)
    if choice == 1:
        await bot.say("Heads")
        print(currentTime() + " - Coin Requested by " + str(author) + " It Landed on Heads!")
    elif choice == 2:
        await bot.say("Tails")
        print(currentTime() + " - Coin Requested by " + str(author) + " It Landed on Tails!")
    

    I have tried looking online at some other questions but I get quite confused looking at them as there is no clear explanation as to what is happening and how I can configure it to work for my code.