How to use _log_level method in robotframework-appiumlibrary

Best Python code snippet using robotframework-appiumlibrary_python

log_utils.py

Source:log_utils.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2"""3:Module: khoros.utils.log_utils4:Synopsis: Collection of logging utilities and functions5:Usage: ``from khoros.utils import log_utils``6:Example: ``logger = log_utils.initialize_logging(__name__)``7:Created By: Jeff Shurtliff8:Last Modified: Jeff Shurtliff9:Modified Date: 08 Jan 202110"""11import os12import sys13import logging14import logging.handlers15from pathlib import Path16LOGGING_DEFAULTS = {17 'logger_name': __name__,18 'log_level': 'info',19 'formatter': logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s'),20 'date_format': '%Y-%m-%d %I:%M:%S',21}22HANDLER_DEFAULTS = {23 'file_log_level': 'info',24 'console_log_level': 'warning',25 'syslog_log_level': 'info',26 'syslog_address': 'localhost',27 'syslog_port': 514,28}29def initialize_logging(logger_name=None, log_level=None, formatter=None, debug=None, no_output=None, file_output=None,30 file_log_level=None, log_file=None, overwrite_log_files=None, console_output=None,31 console_log_level=None, syslog_output=None, syslog_log_level=None, syslog_address=None,32 syslog_port=None):33 # TODO: Add docstring34 logger_name, log_levels, formatter = _apply_defaults(logger_name, formatter, debug, log_level, file_log_level,35 console_log_level, syslog_log_level)36 log_level, file_log_level, console_log_level, syslog_log_level = _get_log_levels_from_dict(log_levels)37 logger = logging.getLogger(logger_name)38 logger = _set_logging_level(logger, log_level)39 logger = _add_handlers(logger, formatter, no_output, file_output, file_log_level, log_file, overwrite_log_files,40 console_output, console_log_level, syslog_output, syslog_log_level, syslog_address,41 syslog_port)42 return logger43class LessThanFilter(logging.Filter):44 """This class allows filters to be set to limit log levels to only less than a specified level.45 .. versionadded:: 3.0.046 .. seealso:: `Zoey Greer <https://stackoverflow.com/users/5124424/zoey-greer>`_ is the original author of47 this class which was provided on `Stack Overflow <https://stackoverflow.com/a/31459386>`_.48 """49 def __init__(self, exclusive_maximum, name=""):50 """This method instantiates the :py:class:`khoros.utils.log_utils.LessThanFilter` class object.51 .. versionadded:: 3.0.052 """53 super(LessThanFilter, self).__init__(name)54 self.max_level = exclusive_maximum55 def filter(self, record):56 """This method returns a Boolean integer value indicating whether or not a message should be logged.57 .. versionadded:: 3.0.058 .. note:: A non-zero return indicates that the message will be logged.59 """60 return 1 if record.levelno < self.max_level else 061def _apply_defaults(_logger_name, _formatter, _debug, _log_level, _file_level, _console_level, _syslog_level):62 """This function applies default values to the configuration settings if not explicitly defined.63 .. versionadded:: 3.0.064 :param _logger_name: The name of the logger instance65 :type _logger_name: str, None66 :param _formatter: The log format to utilize for the logger instance67 :type _formatter: str, None68 :param _debug: Defines if debug mode is enabled69 :type _debug: bool, None70 :param _log_level: The general logging level for the logger instance71 :type _log_level: str, None72 :returns: The values that will be used for the configuration settings73 """74 _log_levels = {75 'general': _log_level,76 'file': _file_level,77 'console': _console_level,78 'syslog': _syslog_level,79 }80 _logger_name = LOGGING_DEFAULTS.get('logger_name') if not _logger_name else _logger_name81 if _debug:82 for _log_type in _log_levels:83 _log_levels[_log_type] = 'debug'84 else:85 if _log_level:86 for _lvl_type, _lvl_value in _log_levels.items():87 if _lvl_type != 'general' and _lvl_value is None:88 _log_levels[_lvl_type] = _log_level89 else:90 _log_level = LOGGING_DEFAULTS.get('log_level')91 if _formatter and isinstance(_formatter, str):92 _formatter = logging.Formatter(_formatter)93 _formatter = LOGGING_DEFAULTS.get('formatter') if not _formatter else _formatter94 return _logger_name, _log_levels, _formatter95def _get_log_levels_from_dict(_log_levels):96 """This function returns the individual log level values from a dictionary.97 .. versionadded:: 3.0.098 :param _log_levels: Dictionary containing log levels for different handlers99 :type _log_levels: dict100 :returns: Individual string values for each handler101 """102 _general = _log_levels.get('general')103 _file = _log_levels.get('file')104 _console = _log_levels.get('console')105 _syslog = _log_levels.get('syslog')106 return _general, _file, _console, _syslog107def _set_logging_level(_logger, _log_level):108 """This function sets the logging level for a :py:class:`logging.Logger` instance.109 .. versionadded:: 3.0.0110 :param _logger: The :py:class:`logging.Logger` instance111 :type _logger: Logger112 :param _log_level: The log level as a string (``debug``, ``info``, ``warning``, ``error`` or ``critical``)113 :type _log_level: str114 :returns: The :py:class:`logging.Logger` instance with a logging level set where applicable115 """116 if _log_level == 'debug':117 _logger.setLevel(logging.DEBUG)118 elif _log_level == 'info':119 _logger.setLevel(logging.INFO)120 elif _log_level == 'warning':121 _logger.setLevel(logging.WARNING)122 elif _log_level == 'error':123 _logger.setLevel(logging.ERROR)124 elif _log_level == 'critical':125 _logger.setLevel(logging.CRITICAL)126 return _logger127def _add_handlers(_logger, _formatter, _no_output, _file_output, _file_log_level, _log_file, _overwrite_log_files,128 _console_output, _console_log_level, _syslog_output, _syslog_log_level, _syslog_address,129 _syslog_port):130 # TODO: Add docstring131 if _no_output or not any((_file_output, _console_output, _syslog_output)):132 _logger.addHandler(logging.NullHandler())133 else:134 if _file_output:135 # Add the FileHandler to the Logger object136 _logger = _add_file_handler(_logger, _file_log_level, _log_file, _overwrite_log_files, _formatter)137 if _console_output:138 # Add the StreamHandler to the Logger object139 _logger = _add_stream_handler(_logger, _console_log_level, _formatter)140 if _syslog_output:141 # Add the SyslogHandler to the Logger object142 _logger = _add_syslog_handler(_logger, _syslog_log_level, _formatter, _syslog_address, _syslog_port)143 return _logger144def _add_file_handler(_logger, _log_level, _log_file, _overwrite, _formatter):145 """This function adds a :py:class:`logging.FileHandler` to the :py:class:`logging.Logger` instance.146 .. versionadded:: 3.0.0147 :param _logger: The :py:class:`logging.Logger` instance148 :type _logger: Logger149 :param _log_level: The log level to set for the handler150 :type _log_level: str151 :param _log_file: The log file (as a file name or a file path) to which messages should be written152 .. note:: If a file path isn't provided then the default directory is the home directory of the user instantiating153 the :py:class:`logging.Logger` object. If a file name is also no provided then it will default to154 using ``khoros.log`` as the file name.155 :param _overwrite: Determines if messages should be appended to the file (default) or overwrite it156 :type _overwrite: bool157 :param _formatter: The :py:class:`logging.Formatter` to apply to messages passed through the handler158 :type _formatter: Formatter159 :returns: The :py:class:`logging.Logger` instance with the added :py:class:`logging.FileHandler`160 """161 # Define the log file to use162 _home_dir = str(Path.home())163 if _log_file:164 if not any((('/' in _log_file), ('\\' in _log_file))):165 _log_file = os.path.join(_home_dir, _log_file)166 else:167 _log_file = os.path.join(_home_dir, 'khoros.log')168 # Identify if log file should be overwritten169 _write_mode = 'w' if _overwrite else 'a'170 # Instantiate the handler171 _handler = logging.FileHandler(_log_file, _write_mode)172 _log_level = HANDLER_DEFAULTS.get('file_log_level') if not _log_level else _log_level173 _handler = _set_logging_level(_handler, _log_level)174 _handler.setFormatter(_formatter)175 # Add the handler to the logger176 _logger.addHandler(_handler)177 return _logger178def _add_stream_handler(_logger, _log_level, _formatter):179 """This function adds a :py:class:`logging.StreamHandler` to the :py:class:`logging.Logger` instance.180 .. versionadded:: 3.0.0181 :param _logger: The :py:class:`logging.Logger` instance182 :type _logger: Logger183 :param _log_level: The log level to set for the handler184 :type _log_level: str185 :param _formatter: The :py:class:`logging.Formatter` to apply to messages passed through the handler186 :type _formatter: Formatter187 :returns: The :py:class:`logging.Logger` instance with the added :py:class:`logging.StreamHandler`188 """189 _log_level = HANDLER_DEFAULTS.get('console_log_level') if not _log_level else _log_level190 _stdout_levels = ['DEBUG', 'INFO']191 if _log_level.upper() in _stdout_levels:192 _logger = _add_split_stream_handlers(_logger, _log_level, _formatter)193 else:194 _handler = logging.StreamHandler()195 _handler = _set_logging_level(_handler, _log_level)196 _handler.setFormatter(_formatter)197 _logger.addHandler(_handler)198 return _logger199def _add_split_stream_handlers(_logger, _log_level, _formatter):200 """This function splits messages into q ``stdout`` or ``stderr`` handler depending on the log level.201 .. versionadded:: 3.0.0202 .. seealso:: Refer to the documentation for the :py:class:`khoros.utils.log_utils.LessThanFilter` for203 more information on how this filtering is implemented and for credit to the original author.204 :param _logger: The :py:class:`logging.Logger` instance205 :type _logger: Logger206 :param _log_level: The log level provided for the stream handler (i.e. console output)207 :type _log_level: str208 :param _formatter: The :py:class:`logging.Formatter` to apply to messages passed through the handlers209 :type _formatter: Formatter210 :returns: The logger instance with the two handlers added211 """212 # Configure and add the STDOUT handler213 _stdout_handler = logging.StreamHandler(sys.stdout)214 _stdout_handler = _set_logging_level(_stdout_handler, _log_level)215 _stdout_handler.addFilter(LessThanFilter(logging.WARNING))216 _stdout_handler.setFormatter(_formatter)217 _logger.addHandler(_stdout_handler)218 # Configure and add the STDERR handler219 _stderr_handler = logging.StreamHandler(sys.stderr)220 _stderr_handler.setLevel(logging.WARNING)221 _stderr_handler.setFormatter(_formatter)222 _logger.addHandler(_stderr_handler)223 # Return the logger with the added handlers224 return _logger225def _add_syslog_handler(_logger, _log_level, _formatter, _address, _port):226 # TODO: Add docstring227 _log_level = HANDLER_DEFAULTS.get('syslog_log_level') if not _log_level else _log_level228 _address = HANDLER_DEFAULTS.get('syslog_address') if not _address else _address229 _port = HANDLER_DEFAULTS.get('syslog_port') if not _port else _port230 _handler = logging.handlers.SysLogHandler(address=(_address, _port))231 _handler = _set_logging_level(_handler, _log_level)232 _handler.setFormatter(_formatter)233 _logger.addHandler(_handler)...

Full Screen

Full Screen

logging.py

Source:logging.py Github

copy

Full Screen

1import logging2__all__ = [3 'get_logger', 'set_logfile', 'set_loglevel', 4 'disable', 'enable'5]6LOGGER_NAME = 'blitzen'7LOGGING_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'8_logger = None9_log_level = logging.INFO10def get_logger():11 global _logger12 global _log_level13 if not _logger:14 _logger = logging.getLogger(LOGGER_NAME)15 formatter = logging.Formatter(16 '%(asctime)s %(name)-8s %(levelname)-8s %(message)s',17 datefmt='%Y-%m-%d %H:%M:%S'18 )19 handler = logging.StreamHandler()20 handler.setFormatter(formatter)21 _logger.addHandler(handler)22 _logger.setLevel(_log_level)23 return _logger24def set_logfile(filename):25 global _logger26 global _log_level27 if _logger:28 fh = logging.FileHandler(filename)29 fh.setLevel(_log_level)30 _logger.addHandler(fh)31def set_loglevel(level):32 global _logger33 global _log_level34 if not _logger:35 _log_level = level36 _logger.setLevel(_log_level)37def disable():38 global _logger39 if not _logger:40 _logger = get_logger()41 _logger.disabled = True42def enable():43 global _logger44 if not _logger:45 _logger = get_logger()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run robotframework-appiumlibrary automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful