我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用logbook.FileHandler()。
def setup_logbook(logfile, logfile_kwargs=None): """Return a basic `logbook` setup which logs to `stderr` and to file.""" if logfile_kwargs is None: logfile_kwargs = {} logfile_kwargs.setdefault('level', 'DEBUG') logfile_kwargs.setdefault('mode', 'w') logfile_kwargs.setdefault('bubble', True) logfile_kwargs.setdefault('format_string', ('--------------------------------------------------------------------------\n' '[{record.time} {record.level_name:<8s} {record.channel:>10s}]' ' {record.filename:s}:{record.lineno:d}\n{record.message:s}')) logbook_setup = logbook.NestedSetup([ logbook.NullHandler(), logbook.more.ColorizedStderrHandler(level='INFO', bubble=False, format_string='[{record.level_name:<8s} {record.channel:s}] {record.message:s}'), logbook.FileHandler(logfile, **logfile_kwargs), ]) return logbook_setup
def setup_logger(test, path='test.log'): test.log_handler = FileHandler(path) test.log_handler.push_application()
def logging_context(path=None, level=None): from logbook import StderrHandler, FileHandler from logbook.compat import redirected_logging with StderrHandler(level=level or 'INFO').applicationbound(): if path: if not os.path.isdir(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) with FileHandler(path, bubble=True).applicationbound(): with redirected_logging(): yield else: with redirected_logging(): yield
def run(self): log = Logger("GAF Bot") log.handlers.append(StreamHandler(sys.stdout, bubble=True)) log.handlers.append(FileHandler("bot/logs/last-run.log", bubble=True, mode="w")) self.logger = log self.logger.notice("Logging started") self.logger.notice("Bot process started") with open("bot/config/defaults/default.guildconfig.json") as f: self.default_guild_config = json.load(f) self.logger.debug("Loaded default guild config") self.logger.debug("Connecting to DB") self.db_conn = sqlite3.connect("bot/config/guild_configs.db") self.logger.notice("DB Connection Established") self.db_cursor = self.db_conn.cursor() self.db_cursor.execute("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='serverSettings'") exists = self.db_cursor.fetchone() if not exists[0]: self.logger.error("No table found in DB! Creating new one now") self.db_cursor.execute('''CREATE TABLE serverSettings (id bigint, settings long)''') self.logger.debug("Table created") self.load_extension("bot.modules.core") self.logger.notice("Loaded core module") self.logger.notice("Loading other modules") # This bar and the time.sleep() stuff is entirely useless # Like completely # Don't do this # It just looks cool and that makes me happy but really this is terrible # and a complete waste of time time.sleep(0.5) for cog in tqdm.tqdm(self.config["modules"].keys(), desc="Loading modules" ): self.load_extension(f"bot.modules.{cog.lower()}") time.sleep(0.2) time.sleep(0.5) self.logger.debug("Completed loading modules") self.logger.notice("Logging into Discord") super().run(self.config["token"], reconnect=True)