Use multiple logging handlers so we can have messages going to both

the console and a file, with differing formats and levels.
This commit is contained in:
Dave Page 2014-12-16 17:14:48 +00:00
parent 2d21ea059b
commit ba939f27d7
2 changed files with 50 additions and 13 deletions

View File

@ -11,6 +11,10 @@
from logging import * from logging import *
##########################################################################
# Log settings
##########################################################################
# Application log level - one of: # Application log level - one of:
# CRITICAL 50 # CRITICAL 50
# ERROR 40 # ERROR 40
@ -19,13 +23,27 @@ from logging import *
# INFO 20 # INFO 20
# DEBUG 10 # DEBUG 10
# NOTSET 0 # NOTSET 0
PGADMIN_LOG_LEVEL = DEBUG CONSOLE_LOG_LEVEL = WARNING
FILE_LOG_LEVEL = DEBUG
# Log format.
CONSOLE_LOG_FORMAT='%(asctime)s: %(levelname)s\t%(name)s:\t%(message)s'
FILE_LOG_FORMAT='%(asctime)s: %(levelname)s\t%(name)s:\t%(message)s'
# Log file name # Log file name
PGADMIN_LOG_FILE = 'pgadmin4.log' LOG_FILE = 'pgadmin4.log'
# Log format. See ##########################################################################
PGADMIN_LOG_FORMAT='%(asctime)s: %(levelname)s\t%(name)s:\t%(message)s' # Server settings
##########################################################################
# The default port on which the app server will listen if not set in the
# environment by the runtime
DEFAULT_SERVER_PORT = 5050
##########################################################################
# Local config settings
##########################################################################
# Load local config overrides # Load local config overrides
try: try:

View File

@ -5,30 +5,47 @@
# Copyright (C) 2013 - 2014, The pgAdmin Development Team # Copyright (C) 2013 - 2014, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence # This software is released under the PostgreSQL Licence
# #
# pgAdmin4.py - Main application entry point # pgadmin4.py - Main application entry point
# #
########################################################################## ##########################################################################
import os, sys, inspect
from time import time, ctime
from flask import Flask
import logging import logging
import os, sys
from flask import Flask
from time import time, ctime
# We need to include the root directory in sys.path to ensure that we can # We need to include the root directory in sys.path to ensure that we can
# find everything we need when running in the standalone runtime. # find everything we need when running in the standalone runtime.
sys.path.append(os.path.dirname(__file__)) sys.path.append(os.path.dirname(__file__))
# Configuration settings # Configuration settings
from config import * import config
# Setup the app object # Setup the app object
app = Flask(__name__) app = Flask(__name__)
#
# Setup logging and log the application startup # Setup logging and log the application startup
#
logging.addLevelName(25, 'SQL') logging.addLevelName(25, 'SQL')
logging.basicConfig(filename=PGADMIN_LOG_FILE, format=PGADMIN_LOG_FORMAT, level=logging.DEBUG) app.logger.setLevel(logging.DEBUG)
app.logger.setLevel(PGADMIN_LOG_LEVEL)
# File logging
fh = logging.FileHandler(config.LOG_FILE)
fh.setLevel(config.FILE_LOG_LEVEL)
fh.setFormatter(logging.Formatter(config.FILE_LOG_FORMAT))
app.logger.addHandler(fh)
# Console logging
ch = logging.StreamHandler()
ch.setLevel(config.CONSOLE_LOG_LEVEL)
ch.setFormatter(logging.Formatter(config.CONSOLE_LOG_FORMAT))
app.logger.addHandler(ch)
app.logger.debug('################################################################################')
app.logger.debug('Starting pgAdmin 4...') app.logger.debug('Starting pgAdmin 4...')
app.logger.debug('################################################################################')
# The main index page # The main index page
@app.route("/") @app.route("/")
@ -52,13 +69,15 @@ def ping():
# runtime if we're running in desktop mode, otherwise we'll just use the # runtime if we're running in desktop mode, otherwise we'll just use the
# Flask default. # Flask default.
if 'PGADMIN_PORT' in globals(): if 'PGADMIN_PORT' in globals():
app.logger.debug('PGADMIN_PORT set in the runtime environment to %s', PGADMIN_PORT)
server_port = PGADMIN_PORT server_port = PGADMIN_PORT
else: else:
server_port = 5000 app.logger.debug('PGADMIN_PORT is not set in the runtime environment, using default of %s', config.DEFAULT_SERVER_PORT)
server_port = config.DEFAULT_SERVER_PORT
if __name__ == '__main__': if __name__ == '__main__':
try: try:
app.run(port=server_port) app.run(port=server_port)
except IOError: except IOError:
print "Unexpected error: ", sys.exc_info()[0] app.logger.error("Error starting the web server: %s", sys.exc_info())