rahul singh
rahul singh

Reputation: 369

how to make a configuration file in python Flask for Logging file

Hello guys i have a configuration file named as "LoggingConfig" containing all data please Before dislike this code tell me what you need coze i don't know how to implement the config and use in Flask program.**so please help **This is the config file i have created:

import logging
#  create and configure logger

    def logger():
        logger=logging.getLogger("DATA-MANUFACTURING") 
        logger.setLevel(logging.DEBUG)

        # create console handler and set level to debug
        ch=logging.StreamHandler()

        # create formatter
        lOG_FORMAT= "%(levelname)s - %(name)s - %(asctime)s - %(message)s"
        logging.basicConfig(filename="C:\\Users\\rahul\\Desktop\\DATA-MANUFACTURING.log", level = ch.setLevel(logging.DEBUG), format = lOG_FORMAT, filemode = "a")

I want to import to my flask program for this i have written a prog like this:

import logging
import LoggingConfig
import pandas as pd
import numpy as np
import random
import os
from random import randint
from flask import Flask, render_template, request, redirect, make_response
LoggingConfig.logger()

app = Flask(__name__)

APP_ROOT = os.path.dirname(os.path.abspath(__file__))
..................................
..................................
so on.........
if __name__ == '__main__':
    app.run(debug=True)

my program have lots of things which i have not given here the only thing i want to make a config file get access in my flask program please if you want me to write whole program here please txt me in text box will add that...thanks

Upvotes: 1

Views: 3415

Answers (1)

snehil singh
snehil singh

Reputation: 554

There are different way of configure Logging Files: Go This site for more info : https://docs.python-guide.org/writing/logging/ For You i prefer INI- format Go to your program make one more file named as "Yourfilename.ini"

[loggers]
keys=root,DATA-MANUFACTURING

[handlers]
keys=consoleHandler,fileHandler

[formatters]
keys=fileFormatter,consoleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_DATA-MANUFACTURING]
level=DEBUG
handlers=consoleHandler,fileHandler
qualname=DATA-MANUFACTURING
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=WARNING
formatter=consoleFormatter
args=(sys.stdout,)

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=fileFormatter
args=('logfile.log', 'a')

[formatter_fileFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

[formatter_consoleFormatter]
format=%(levelname)s - %(message)s
datefmt=

Now in your main program write on the top:

import logging.config
logging.config.fileConfig( 'logging.ini') 
logger = logging.getLogger('DATA-MANUFACTURING')
.........................................

and write any logging info whatever you want

logger.info("Loading the UpoadFile page")...so on.

Upvotes: 3

Related Questions