<commit_before><commit_msg>Add a client plugin to log to files<commit_after>import logging
import os

import king_phisher.client.application as application
import king_phisher.client.plugins as plugins
import king_phisher.client.gui_utilities as gui_utilities

# logger name value
LOGGER_NAME = ''

# log file size, in MB
LOG_FILE_SIZE = 10

class Plugin(plugins.ClientPlugin):
	authors = ['<NAME>']
	title = 'Logger'
	description = """
	Keep logs of campaign feedback and results. The directory
	of the logged file(s) is $HOME/.config/king-phisher.
	"""
	homepage = 'https://github.com/securestate/king-phisher-plugins'

	# this is the primary plugin entry point which is executed when the plugin is enabled
	def initialize(self):
		# ensure the directory for the logs exists
		log_dir = application.USER_DATA_PATH
		if not os.path.exists(log_dir):
			os.mkdir(log_dir)

		# convert the specified log file size (MB) to bytes for use by the logger
		file_size = LOG_FILE_SIZE * 1024 * 1024

		# grab the logger in use by the client (root logger)
		logger = logging.getLogger(LOGGER_NAME)

		# set up the handler and formatter for the logger, and attach the components
		handler = logging.handlers.RotatingFileHandler(os.path.join(log_dir, 'client_log.log'), maxBytes=file_size, backupCount=2)
		formatter = logging.Formatter('%(asctime)s %(name)-50s %(levelname)-8s %(message)s')
		handler.setFormatter(formatter)
		logger.addHandler(handler)

		# keep reference of handler as an attribute
		self.handler = handler

		return True

	# this is a cleanup method to allow the plugin to close any open resources
	def finalize(self):
		# remove the logging handler from the logger and close it
		logger = logging.getLogger(LOGGER_NAME)
		logger.removeHandler(self.handler)
		self.handler.flush()
		self.handler.close()

{'fraction_non_alphanumeric': 0.08659680088251517, 'fraction_numerical': 0.007722007722007722, 'mean_word_length': 4.633540372670807, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3672723', 'n_tokens_mistral': 584, 'n_tokens_neox': 540, 'n_words': 185}