Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import logging
- import sqlite3
- def DB_init():
- conn = sqlite3.connect("gachimuchnik.db")
- cursor = conn.cursor()
- # cursor.execute("""CREATE TABLE log
- # (title text, main text)
- # """)
- conn.commit()
- class Logger:
- def __init__(self, strategy):
- self.strategy = strategy
- def log_somewhere(self, text):
- return self.strategy.log(text)
- def set_strategy(self, strategy):
- self.strategy = strategy
- class Strategy:
- def log(self, text):
- print('abstract log', text)
- class DBLogging(Strategy):
- def log(self, text):
- conn = sqlite3.connect("gachimuchnik.db")
- cursor = conn.cursor()
- cursor.execute("""INSERT INTO log
- VALUES (?, ?)""",
- ("DB: ", text)
- )
- conn.commit()
- conn.close()
- class FileLogging:
- def log(self, text):
- logging.basicConfig(filename="wassap.log", level=logging.INFO)
- logging.info("Program started")
- class ConsoleLogging:
- def log(self, text):
- print('Console: ', text)
- def main():
- DB_init()
- logger = Logger(DBLogging())
- logger.log_somewhere('Zdarova')
- logger = Logger(FileLogging())
- logger.log_somewhere('Zdarova')
- logger = Logger(ConsoleLogging())
- logger.log_somewhere('Zdarova')
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement