Skip to content

Commit

Permalink
Revert "Fix logging (#898)"
Browse files Browse the repository at this point in the history
This reverts commit ccea413.
  • Loading branch information
mdipierro committed Aug 10, 2024
1 parent f5aa58b commit 1c98ced
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 100 deletions.
97 changes: 95 additions & 2 deletions py4web/server_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
except ImportError:
wsservers_list = []

from .utils.logsetting import get_log_file, check_level, logging_conf

__all__ = [
"gunicorn",
"gunicornGevent",
Expand All @@ -26,6 +24,101 @@
# ---------------------- utils -----------------------------------------------


# export PY4WEB_LOGS=/tmp # export PY4WEB_LOGS=
def get_log_file(out_banner=True):
log_dir = os.environ.get("PY4WEB_LOGS", None)
if log_dir and os.path.isdir(log_dir):
log_file = os.path.join(log_dir, "server-py4web.log")
if out_banner:
print(f"log_file: {log_file}")
return log_file
return None


def check_level(level):

# lib/python3.7/logging/__init__.py
# CRITICAL = 50
# FATAL = CRITICAL
# ERROR = 40
# WARNING = 30
# WARN = WARNING
# INFO = 20
# DEBUG = 10
# NOTSET = 0

return (
level
if level
in (
logging.CRITICAL,
logging.ERROR,
logging.WARN,
logging.INFO,
logging.DEBUG,
logging.NOTSET,
)
else logging.WARN
)


def logging_conf(level=logging.WARN, logger_name=__name__, fmode="w", test_log=False):

log_file = get_log_file()
log_to = dict()

if log_file:
if sys.version_info >= (3, 9):
log_to["filename"] = log_file
log_to["filemode"] = fmode
log_to["encoding"] = "utf-8"
else:
try:
h = logging.FileHandler(log_file, mode=fmode, encoding="utf-8")
log_to.update({"handlers": [h]})
except (LookupError, KeyError, ValueError) as ex:
print(f"{ex}, bad encoding {__file__}")
pass

short_msg = "%(message)s > %(threadName)s > %(asctime)s.%(msecs)03d"
# long_msg = short_msg + " > %(funcName)s > %(filename)s:%(lineno)d > %(levelname)s"

time_msg = "%H:%M:%S"
# date_time_msg = '%Y-%m-%d %H:%M:%S'

try:
logging.basicConfig(
format=short_msg,
datefmt=time_msg,
level=check_level(level),
**log_to,
)
except (OSError, LookupError, KeyError, ValueError) as ex:
print(f"{ex}, {__file__}")
print(f"cannot open {log_file}")
logging.basicConfig(
format="%(message)s",
level=check_level(level),
)

if logger_name is None:
return None

log = logging.getLogger("SA:" + logger_name)
log.propagate = True

if test_log:
for func in (
log.debug,
log.info,
log.warn,
log.error,
log.critical,
):
func("func: " + func.__name__)

return log


def get_workers(opts, default=10):
try:
Expand Down
98 changes: 0 additions & 98 deletions py4web/utils/logsetting.py

This file was deleted.

0 comments on commit 1c98ced

Please sign in to comment.