def test_find_log_dir_with_nothing(self): with mock.patch.object(os.path, 'exists'), \ mock.patch.object(os.path, 'isdir'): os.path.exists.return_value = False os.path.isdir.return_value = False exception_class = OSError if six.PY2 else FileNotFoundError with self.assertRaises(exception_class): logging.find_log_dir()
def test_find_log_dir_with_flag(self): with mock.patch.object(os, 'access'), \ mock.patch.object(os.path, 'isdir'): os.path.isdir.return_value = True os.access.return_value = True log_dir = logging.find_log_dir() self.assertEqual('./', log_dir)
def setup_logging(): """Logging related setup. Creates the log directory if it does not exist and sets up the output of logging to use absl log file. """ logging.set_verbosity(logging.INFO) log_dir = '' if FLAGS.log_dir: log_dir = FLAGS.log_dir + LOG_FOLDER_NAME else: log_dir = logging.find_log_dir() + LOG_FOLDER_NAME if FLAGS.enable_mount_directory: log_dir = MOUNT_DIR + log_dir try: if not os.path.exists(log_dir): os.makedirs(log_dir) logging.get_absl_handler().use_absl_log_file('', log_dir) except OSError as err: logging.error('Failed to create log directory %s, err %s', log_dir, str(err)) except Exception as ex: # pylint: disable=broad-except logging.error('Unknown exception occurs in setup_logging, err: %s', str(ex))
def update_url(process): """Get the agent update source URL. If there is no agent running locally, return the stable agent URL. Otherwise, read the agent source text file to get the update source URL. Args: process: Agent process or None if there is no currently running process. Returns: Agent update source URL string. """ if process is None: return FLAGS.stable_agent_url filename = logging.find_log_dir() + 'agent_source_%d.txt' % process.pid try: f = open(filename, 'r') url = f.read() if is_valid_url(url): return url else: logging.error('URL is not valid, using default stable URL.') return FLAGS.stable_agent_url except IOError: return FLAGS.stable_agent_url
def test_find_log_dir_with_nothing(self): with mock.patch.object(os.path, 'exists'), \ mock.patch.object(os.path, 'isdir'), \ mock.patch.object(logging.get_absl_logger(), 'fatal') as mock_fatal: os.path.exists.return_value = False os.path.isdir.return_value = False log_dir = logging.find_log_dir() mock_fatal.assert_called() self.assertEqual(None, log_dir)
def test_find_log_dir_with_tmp(self): with mock.patch.object(os, 'access'), \ mock.patch.object(os.path, 'exists'), \ mock.patch.object(os.path, 'isdir'): os.path.exists.return_value = False os.path.isdir.side_effect = lambda path: path == '/tmp/' os.access.return_value = True log_dir = logging.find_log_dir() self.assertEqual('/tmp/', log_dir)
def delete_agent_source_file(process): if process is None: return try: filename = logging.find_log_dir( ) + '/agent_source_%d.txt' % process.pid if os.path.exists(filename): os.remove(filename) except OSError as err: logging.error('Failed to delete file %s, error: %s', filename, str(err))
import sys from absl import app from absl import flags from absl import logging import testtemp import filetemp.testtemp FLAGS = flags.FLAGS FLAGS.alsologtostderr = True FLAGS.log_dir = 'temp/log/' print("log_dir: ", logging.find_log_dir()) print("actual_log_dir, file_prefix, program_name: ", logging.find_log_dir_and_names('main')) flags.DEFINE_string('log_file', 'main', 'Text to echo.') def main(argv): logging.get_absl_handler().start_logging_to_file(FLAGS.log_file) print("logging path: ", logging.get_log_file_name()) logging.info('Running under Python {0[0]}.{0[1]}.{0[2]}'.format( sys.version_info)) logging.info("logging level: %d" % logging.get_verbosity()) for i in range(50):