Advertisement
namemkazaza

Logger

Feb 1st, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. import logging
  2. import sqlite3
  3.  
  4.  
  5. def DB_init():
  6.     conn = sqlite3.connect("gachimuchnik.db")
  7.     cursor = conn.cursor()
  8.     # cursor.execute("""CREATE TABLE log
  9.     # (title text, main text)
  10.     # """)
  11.     conn.commit()
  12.  
  13. class Logger:
  14.     def __init__(self, strategy):
  15.         self.strategy = strategy
  16.  
  17.     def log_somewhere(self, text):
  18.         return self.strategy.log(text)
  19.  
  20.     def set_strategy(self, strategy):
  21.         self.strategy = strategy
  22.  
  23.  
  24. class Strategy:
  25.     def log(self, text):
  26.         print('abstract log', text)
  27.  
  28.  
  29. class DBLogging(Strategy):
  30.     def log(self, text):
  31.         conn = sqlite3.connect("gachimuchnik.db")
  32.         cursor = conn.cursor()
  33.         cursor.execute("""INSERT INTO log
  34.        VALUES (?, ?)""",
  35.                        ("DB: ", text)
  36.                        )
  37.         conn.commit()
  38.         conn.close()
  39.  
  40.  
  41. class FileLogging:
  42.     def log(self, text):
  43.         logging.basicConfig(filename="wassap.log", level=logging.INFO)
  44.         logging.info("Program started")
  45.  
  46.  
  47. class ConsoleLogging:
  48.     def log(self, text):
  49.         print('Console: ', text)
  50.  
  51.  
  52. def main():
  53.     DB_init()
  54.     logger = Logger(DBLogging())
  55.     logger.log_somewhere('Zdarova')
  56.     logger = Logger(FileLogging())
  57.     logger.log_somewhere('Zdarova')
  58.     logger = Logger(ConsoleLogging())
  59.     logger.log_somewhere('Zdarova')
  60.  
  61.  
  62. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement