Knowledge Base
main
main
  • Hello , There !!!
  • Blogs , Thoughts , Etc
    • Blogs
      • What happens when you type a URL into your browser?
      • HTTP Keep Alive Vs TCP keep Alive
      • How Browsers Work ?
      • Change Data Capture @ MySQL
    • Batch Vs Stream Processing
    • Book Summaries
      • 12 Rules for Life
    • Random Thoughts
      • Being On The Right Side Of The “Zero”
      • I ̶F̶̶a̶̶i̶̶l̶̶e̶̶d̶ Learnt Something New.
  • NODE JS
    • Parsing HTML DOM
    • Make HTTP Request
    • Read SQLite File
    • Remove Directory Recursively
  • JavaScript
    • Event Loop In JavaScript
  • Numbers
    • Untitled
  • Random
    • Google File System
    • Concurrency vs Parallelism
    • Spark
    • Apache Kafka[WIP]
  • Python
    • Python Memory Model
    • Multithreading With LightWeight Threads
    • Logger
  • JAVA
    • Java Memory Model
  • SQL
    • Untitled
    • Page 1
  • Machine Learning
    • Page 2
  • Productivity
    • Git
    • ssh
  • Hacks
    • Good Youtube Channels
Powered by GitBook
On this page

Was this helpful?

  1. Python

Logger

import logging
from datetime import datetime


class Logger:
    __instance = None

    @staticmethod
    def getInstance():
        if Logger.__instance is None:
            Logger()
        return Logger.__instance

    def __init__(self):
        if Logger.__instance is not None:
            raise Exception("This class is a singleton!")
        else:
            Logger.__instance = self
            logging.getLogger("urllib3").setLevel(logging.WARNING)
            logging.root.setLevel(logging.NOTSET)
            logging.basicConfig(level=logging.NOTSET)
            self.errorLogger = logging.getLogger('VISUAL-SEARCH-ERROR')
            self.infoLogger = logging.getLogger('VISUAL-SEARCH-INFO')
            self.errorLogFile = 'logs/error/' + str(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
            self.infoLogFile = 'logs/info/' + str(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
            self.doInitialConfig()
            self.logInfo('LOGGER-STARTED')

    def doInitialConfig(self):
        infoHandler = logging.FileHandler(self.infoLogFile)
        infoHandler.setLevel(logging.INFO)
        infoFormat  = logging.Formatter('%(asctime)s - %(name)s - %(message)s')
        infoHandler.setFormatter(infoFormat)
        self.infoLogger.addHandler(infoHandler)

        errorHandler = logging.FileHandler(self.errorLogFile)
        errorHandler.setLevel(logging.ERROR)
        errorFormat = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        errorHandler.setFormatter(errorFormat)
        self.errorLogger.addHandler(errorHandler)

    def logError(self, msg):
        self.errorLogger.error(msg)

    def logInfo(self, msg):
        self.infoLogger.info(msg)Py
PreviousMultithreading With LightWeight ThreadsNextJava Memory Model

Last updated 3 years ago

Was this helpful?