mirror of
https://github.com/gluster/glusterfs.git
synced 2026-02-07 03:48:44 +01:00
see https://review.gluster.org/#/c/19788/, https://review.gluster.org/#/c/19871/, and https://review.gluster.org/#/c/19952/ This patch adds version agnostic imports for urllib, cpickle, socketserver, _thread, queue, etc., suggested by Aravinda in https://review.gluster.org/#/c/19767/1 Note: Fedora packaging guidelines require explicit shebangs, so popular practices like #!/usr/bin/env python and #!/usr/bin/python are not allowed; they must be #!/usr/bin/python2 or #!/usr/bin/python3 Note: Selected small fixes from 2to3 utility. Specifically apply, basestring, funcattrs, idioms, numliterals, set_literal, types, urllib, and zip have already been applied. Note: these 2to3 fixes report no changes are necessary: exec, execfile, exitfunc, filter, getcwdu, intern, itertools, metaclass, methodattrs, ne, next, nonzero, operator, paren, raw_input, reduce, reload, renames, repr, standarderror, sys_exc, throw, tuple_params, xreadlines. Change-Id: I8d393064a1837874d8b4bc87c8ce05c679664642 updates: #411 Signed-off-by: Kaleb S. KEITHLEY <kkeithle@redhat.com>
78 lines
2.3 KiB
Python
78 lines
2.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
#
|
|
# Copyright (c) 2016 Red Hat, Inc. <http://www.redhat.com>
|
|
# This file is part of GlusterFS.
|
|
#
|
|
# This file is licensed to you under your choice of the GNU Lesser
|
|
# General Public License, version 3 or any later version (LGPLv3 or
|
|
# later), or the GNU General Public License, version 2 (GPLv2), in all
|
|
# cases as published by the Free Software Foundation.
|
|
#
|
|
|
|
import logging
|
|
from logging import Logger, handlers
|
|
import sys
|
|
import time
|
|
|
|
|
|
class GLogger(Logger):
|
|
|
|
"""Logger customizations for gsyncd.
|
|
|
|
It implements a log format similar to that of glusterfs.
|
|
"""
|
|
|
|
def makeRecord(self, name, level, *a):
|
|
rv = Logger.makeRecord(self, name, level, *a)
|
|
rv.nsecs = (rv.created - int(rv.created)) * 1000000
|
|
fr = sys._getframe(4)
|
|
callee = fr.f_locals.get('self')
|
|
if callee:
|
|
ctx = str(type(callee)).split("'")[1].split('.')[-1]
|
|
else:
|
|
ctx = '<top>'
|
|
if not hasattr(rv, 'funcName'):
|
|
rv.funcName = fr.f_code.co_name
|
|
rv.lvlnam = logging.getLevelName(level)[0]
|
|
rv.ctx = ctx
|
|
return rv
|
|
|
|
|
|
LOGFMT = ("[%(asctime)s.%(nsecs)d] %(lvlnam)s [%(module)s{0}"
|
|
":%(lineno)s:%(funcName)s] %(ctx)s: %(message)s")
|
|
|
|
|
|
def setup_logging(level="INFO", label="", log_file=""):
|
|
if label:
|
|
label = "(" + label + ")"
|
|
|
|
filename = None
|
|
stream = None
|
|
if log_file:
|
|
if log_file in ('-', '/dev/stderr'):
|
|
stream = sys.stderr
|
|
elif log_file == '/dev/stdout':
|
|
stream = sys.stdout
|
|
else:
|
|
filename = log_file
|
|
|
|
datefmt = "%Y-%m-%d %H:%M:%S"
|
|
fmt = LOGFMT.format(label)
|
|
logging.root = GLogger("root", level)
|
|
logging.setLoggerClass(GLogger)
|
|
logging.Formatter.converter = time.gmtime # Log in GMT/UTC time
|
|
logging.getLogger().handlers = []
|
|
logging.getLogger().setLevel(level)
|
|
|
|
if filename is not None:
|
|
logging_handler = handlers.WatchedFileHandler(filename)
|
|
formatter = logging.Formatter(fmt=fmt,
|
|
datefmt=datefmt)
|
|
logging_handler.setFormatter(formatter)
|
|
logging.getLogger().addHandler(logging_handler)
|
|
else:
|
|
logging.basicConfig(stream=stream,
|
|
format=fmt,
|
|
datefmt=datefmt,
|
|
level=level)
|