提交 0d38a670 编写于 作者: H Hui Zhang

fix logger name

上级 2cc7455b
......@@ -637,7 +637,7 @@
{
"cell_type": "code",
"execution_count": 59,
"id": "diverse-advocate",
"id": "featured-cookbook",
"metadata": {},
"outputs": [
{
......@@ -660,7 +660,7 @@
{
"cell_type": "code",
"execution_count": 35,
"id": "liquid-argument",
"id": "concrete-rubber",
"metadata": {},
"outputs": [
{
......@@ -705,7 +705,7 @@
{
"cell_type": "code",
"execution_count": 3,
"id": "copyrighted-advertising",
"id": "surgical-shore",
"metadata": {},
"outputs": [
{
......@@ -728,7 +728,7 @@
{
"cell_type": "code",
"execution_count": 4,
"id": "compatible-edwards",
"id": "medieval-daily",
"metadata": {},
"outputs": [
{
......@@ -748,7 +748,7 @@
{
"cell_type": "code",
"execution_count": 5,
"id": "general-counter",
"id": "derived-meeting",
"metadata": {},
"outputs": [],
"source": [
......@@ -758,7 +758,7 @@
{
"cell_type": "code",
"execution_count": 6,
"id": "logical-kelly",
"id": "hawaiian-engagement",
"metadata": {},
"outputs": [
{
......@@ -776,7 +776,7 @@
{
"cell_type": "code",
"execution_count": 7,
"id": "metallic-semiconductor",
"id": "moderate-greece",
"metadata": {},
"outputs": [
{
......@@ -809,7 +809,7 @@
{
"cell_type": "code",
"execution_count": 17,
"id": "shaped-oregon",
"id": "duplicate-colon",
"metadata": {},
"outputs": [
{
......@@ -839,7 +839,7 @@
{
"cell_type": "code",
"execution_count": 18,
"id": "loving-chester",
"id": "actual-junior",
"metadata": {},
"outputs": [
{
......@@ -871,7 +871,7 @@
{
"cell_type": "code",
"execution_count": 19,
"id": "annual-kidney",
"id": "looking-belle",
"metadata": {},
"outputs": [
{
......@@ -903,7 +903,7 @@
{
"cell_type": "code",
"execution_count": 31,
"id": "dressed-linux",
"id": "turned-friend",
"metadata": {},
"outputs": [
{
......@@ -935,7 +935,7 @@
{
"cell_type": "code",
"execution_count": 32,
"id": "shared-suspension",
"id": "wrapped-foundation",
"metadata": {},
"outputs": [
{
......@@ -966,7 +966,7 @@
{
"cell_type": "code",
"execution_count": 20,
"id": "invisible-oakland",
"id": "applied-abuse",
"metadata": {},
"outputs": [],
"source": [
......@@ -977,7 +977,7 @@
{
"cell_type": "code",
"execution_count": 46,
"id": "czech-flesh",
"id": "experimental-invasion",
"metadata": {},
"outputs": [
{
......@@ -997,7 +997,7 @@
{
"cell_type": "code",
"execution_count": 30,
"id": "yellow-great",
"id": "established-lunch",
"metadata": {},
"outputs": [],
"source": [
......@@ -1007,7 +1007,7 @@
{
"cell_type": "code",
"execution_count": 31,
"id": "quiet-booking",
"id": "residential-logic",
"metadata": {},
"outputs": [],
"source": [
......@@ -1017,7 +1017,7 @@
{
"cell_type": "code",
"execution_count": 47,
"id": "certified-clinton",
"id": "maritime-consequence",
"metadata": {},
"outputs": [
{
......@@ -1038,7 +1038,7 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "fatal-latvia",
"id": "african-identification",
"metadata": {},
"outputs": [],
"source": [
......@@ -1049,7 +1049,7 @@
{
"cell_type": "code",
"execution_count": 56,
"id": "boxed-damage",
"id": "upset-regard",
"metadata": {},
"outputs": [],
"source": [
......@@ -1059,7 +1059,7 @@
{
"cell_type": "code",
"execution_count": 58,
"id": "empty-latitude",
"id": "wrapped-netherlands",
"metadata": {},
"outputs": [],
"source": [
......@@ -1069,7 +1069,7 @@
{
"cell_type": "code",
"execution_count": 60,
"id": "apart-restriction",
"id": "engaging-statement",
"metadata": {},
"outputs": [],
"source": [
......@@ -1079,7 +1079,7 @@
{
"cell_type": "code",
"execution_count": 66,
"id": "corresponding-relief",
"id": "joined-handy",
"metadata": {},
"outputs": [],
"source": [
......@@ -1089,7 +1089,7 @@
{
"cell_type": "code",
"execution_count": 95,
"id": "vertical-monroe",
"id": "absolute-kenya",
"metadata": {},
"outputs": [
{
......@@ -1110,7 +1110,7 @@
{
"cell_type": "code",
"execution_count": 81,
"id": "handmade-athletics",
"id": "integral-subsection",
"metadata": {},
"outputs": [
{
......@@ -1131,7 +1131,7 @@
{
"cell_type": "code",
"execution_count": 11,
"id": "failing-barrier",
"id": "conservative-sport",
"metadata": {},
"outputs": [],
"source": [
......@@ -1161,7 +1161,7 @@
{
"cell_type": "code",
"execution_count": 12,
"id": "british-perfume",
"id": "connected-brass",
"metadata": {},
"outputs": [],
"source": [
......@@ -1172,7 +1172,7 @@
{
"cell_type": "code",
"execution_count": 14,
"id": "balanced-lawsuit",
"id": "worse-couple",
"metadata": {},
"outputs": [
{
......@@ -1201,7 +1201,7 @@
{
"cell_type": "code",
"execution_count": 13,
"id": "married-wallpaper",
"id": "unlimited-effects",
"metadata": {},
"outputs": [
{
......@@ -1230,7 +1230,7 @@
{
"cell_type": "code",
"execution_count": 15,
"id": "parallel-electric",
"id": "stable-artist",
"metadata": {},
"outputs": [
{
......@@ -1260,7 +1260,7 @@
{
"cell_type": "code",
"execution_count": 29,
"id": "foster-senegal",
"id": "romance-laser",
"metadata": {},
"outputs": [
{
......@@ -1290,7 +1290,7 @@
{
"cell_type": "code",
"execution_count": 30,
"id": "great-shareware",
"id": "pharmaceutical-colombia",
"metadata": {},
"outputs": [
{
......@@ -1308,7 +1308,7 @@
{
"cell_type": "code",
"execution_count": 22,
"id": "collaborative-behavior",
"id": "forty-harvest",
"metadata": {},
"outputs": [
{
......@@ -1343,7 +1343,7 @@
{
"cell_type": "code",
"execution_count": 23,
"id": "moving-trunk",
"id": "thirty-software",
"metadata": {},
"outputs": [
{
......@@ -1373,10 +1373,72 @@
"librosa.display.waveplot(samples_out, sr=sr)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "portuguese-addiction",
"metadata": {},
"outputs": [],
"source": [
"import getpass"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "everyday-vessel",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['GetPassWarning',\n",
" '__all__',\n",
" '__builtins__',\n",
" '__cached__',\n",
" '__doc__',\n",
" '__file__',\n",
" '__loader__',\n",
" '__name__',\n",
" '__package__',\n",
" '__spec__',\n",
" '_raw_input',\n",
" 'contextlib',\n",
" 'fallback_getpass',\n",
" 'getpass',\n",
" 'getuser',\n",
" 'io',\n",
" 'os',\n",
" 'sys',\n",
" 'termios',\n",
" 'unix_getpass',\n",
" 'warnings',\n",
" 'win_getpass']"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dir(getpass)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "latter-expression",
"metadata": {},
"outputs": [],
"source": [
"getpass?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "previous-senegal",
"id": "small-professor",
"metadata": {},
"outputs": [],
"source": []
......@@ -14,20 +14,120 @@
import logging
import os
import sys
import getpass
import socket
import time
def find_log_dir(log_dir=None):
"""Returns the most suitable directory to put log files into.
Args:
log_dir: str|None, if specified, the logfile(s) will be created in that
directory. Otherwise if the --log_dir command-line flag is provided,
the logfile will be created in that directory. Otherwise the logfile
will be created in a standard location.
Raises:
FileNotFoundError: raised when it cannot find a log directory.
"""
# Get a list of possible log dirs (will try to use them in order).
if log_dir:
# log_dir was explicitly specified as an arg, so use it and it alone.
dirs = [log_dir]
else:
dirs = ['/tmp/', './']
# Find the first usable log dir.
for d in dirs:
if os.path.isdir(d) and os.access(d, os.W_OK):
return d
raise FileNotFoundError(
"Can't find a writable directory for logs, tried %s" % dirs)
def find_log_dir_and_names(program_name=None, log_dir=None):
"""Computes the directory and filename prefix for log file.
Args:
program_name: str|None, the filename part of the path to the program that
is running without its extension. e.g: if your program is called
'usr/bin/foobar.py' this method should probably be called with
program_name='foobar' However, this is just a convention, you can
pass in any string you want, and it will be used as part of the
log filename. If you don't pass in anything, the default behavior
is as described in the example. In python standard logging mode,
the program_name will be prepended with py_ if it is the program_name
argument is omitted.
log_dir: str|None, the desired log directory.
Returns:
(log_dir, file_prefix, symlink_prefix)
Raises:
FileNotFoundError: raised in Python 3 when it cannot find a log directory.
OSError: raised in Python 2 when it cannot find a log directory.
"""
if not program_name:
# Strip the extension (foobar.par becomes foobar, and
# fubar.py becomes fubar). We do this so that the log
# file names are similar to C++ log file names.
program_name = os.path.splitext(os.path.basename(sys.argv[0]))[0]
# Prepend py_ to files so that python code gets a unique file, and
# so that C++ libraries do not try to write to the same log files as us.
program_name = 'py_%s' % program_name
actual_log_dir = find_log_dir(log_dir=log_dir)
try:
username = getpass.getuser()
except KeyError:
# This can happen, e.g. when running under docker w/o passwd file.
if hasattr(os, 'getuid'):
# Windows doesn't have os.getuid
username = str(os.getuid())
else:
username = 'unknown'
hostname = socket.gethostname()
file_prefix = '%s.%s.%s.log' % (program_name, hostname, username)
return actual_log_dir, file_prefix, program_name
class Log():
def __init__(self, logger=None, log_cate='global'):
log_name = None
def __init__(self, logger=None):
self.logger = logging.getLogger(logger)
self.logger.setLevel(logging.DEBUG)
file_dir = os.getcwd() + '/log'
if not os.path.exists(file_dir):
os.mkdir(file_dir)
self.log_path = file_dir
self.log_name = self.log_path + "/" + log_cate + '.log'
self.log_dir = file_dir
actual_log_dir, file_prefix, symlink_prefix = find_log_dir_and_names(
program_name=None, log_dir=self.log_dir)
basename = '%s.INFO.%s.%d' % (
file_prefix,
time.strftime('%Y%m%d-%H%M', time.localtime(time.time())),
os.getpid())
filename = os.path.join(actual_log_dir, basename)
if Log.log_name is None:
Log.log_name = filename
# Create a symlink to the log file with a canonical name.
symlink = os.path.join(actual_log_dir, symlink_prefix + '.INFO')
try:
if os.path.islink(symlink):
os.unlink(symlink)
os.symlink(os.path.basename(Log.log_name), symlink)
except EnvironmentError:
# If it fails, we're sad but it's no error. Commonly, this
# fails because the symlink was created by another user and so
# we can't modify it
pass
fh = logging.FileHandler(self.log_name)
fh = logging.FileHandler(Log.log_name)
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册