Skip to content

Commit caec069

Browse files
committed
Add a config setting LogDir used for all log files
Also, allow specifying a different locations of the application config.
1 parent 94c206d commit caec069

8 files changed

+45
-26
lines changed

.pylintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ indent-after-paren = 4
6262

6363
[DESIGN]
6464

65-
max-attributes = 35
65+
max-attributes = 40
6666
max-args = 10
6767
max-branches = 40
6868
max-line-length = 79

docs/appdev.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ Activity Log
281281
Three options let you control:
282282

283283
* Whether or not to log activity (``LogActivity``, defaults to 0, i.e. off)
284-
* The name of the file to store the log (``ActivityLogFilename``, defaults to ``Logs/Activity.csv``)
284+
* The name of the file to store the log (``ActivityLogFilename``, defaults to ``Activity.csv``)
285285
* The fields to store in the log (``ActivityLogColumns``) </ul>
286286

287287
See the chapter on :ref:`configuration` for more information.
@@ -314,7 +314,7 @@ The most common technique is the infamous ``print`` statement which has been rep
314314

315315
For convenient debugging, the default ``Application.config`` file already uses the following conditional setting::
316316

317-
AppLogFilename = None if Development else 'Logs/Application.log'
317+
AppLogFilename = None if Development else 'Application.log'
318318

319319
This will prevent standard output and error from being redirected to the log file in development mode, which makes it easier to find debugging output, and also makes it possible to use ``pdb`` (see below).
320320

docs/config.rst

+5-3
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Errors
158158
``UserErrorMessage``:
159159
This is the error message that is displayed to the user when an uncaught exception escapes a servlet. Default: ``"The site is having technical difficulties with this page. An error has been logged, and the problem will be fixed as soon as possible. Sorry!"``
160160
``ErrorLogFilename``:
161-
The name of the file where exceptions are logged. Each entry contains the date and time, filename, pathname, exception name and data, and the HTML error message filename (assuming there is one). Default: ``Logs/Errors.csv``.
161+
The name of the file where exceptions are logged. Each entry contains the date and time, filename, pathname, exception name and data, and the HTML error message filename (assuming there is one). Default: ``Errors.csv``.
162162
``SaveErrorMessages``:
163163
If True, then errors (e.g., uncaught exceptions) will produce an HTML file with both the user message and debugging information. Developers/administrators can view these files after the fact, to see the details of what went wrong. These error messages can take a surprising amount of space. Default: ``True`` (do save).
164164
``ErrorMessagesDir``:
@@ -209,11 +209,13 @@ Logging
209209
``LogActivity``:
210210
If True, then the execution of each servlet is logged with useful information such as time, duration and whether or not an error occurred. Default: ``True``.
211211
``ActivityLogFilenames``:
212-
This is the name of the file that servlet executions are logged to. This setting has no effect if ``LogActivity`` is False. The path can be relative to the Webware location, or an absolute path. Default: ``"Logs/Activity.csv"``.
212+
This is the name of the file that servlet executions are logged to. This setting has no effect if ``LogActivity`` is False. The path can be relative to the Webware location, or an absolute path. Default: ``"Activity.csv"``.
213213
``ActivityLogColumns``:
214214
Specifies the columns that will be stored in the activity log. Each column can refer to an object from the set [application, transaction, request, response, servlet, session] and then refer to its attributes using "dot notation". The attributes can be methods or instance attributes and can be qualified arbitrarily deep. Default: ``['request.remoteAddress', 'request.method', 'request.uri', 'response.size', 'servlet.name', 'request.timeStamp', 'transaction.duration', 'transaction.errorOccurred']``.
215215
``AppLogFilename``:
216-
The Application redirects standard output and error to this file, if this is set in production mode. Default: ``'Logs/Application.log'``.
216+
The Application redirects standard output and error to this file, if this is set in production mode. Default: ``'Application.log'``.
217+
```LogDir``:
218+
The directory where log files should be stored. All log files without an explicit path will be put here.
217219
``Verbose``:
218220
If True, then additional messages are printed while the Application runs, most notably information about each request such as size and response time. Default: ``True``.
219221
``SilentURIs``:

webware/Admin/AdminPage.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ def writeAdminMenu(self):
2323
self.menuHeading('Admin')
2424
self.menuItem('Home', 'Main')
2525
self.menuItem(
26-
'Activity log', 'Access', self.fileSize('ActivityLogFilename'))
26+
'Activity log', 'Access', self.fileSize('ActivityLog'))
2727
self.menuItem(
28-
'Error log', 'Errors', self.fileSize('ErrorLogFilename'))
28+
'Error log', 'Errors', self.fileSize('ErrorLog'))
2929
self.menuItem('Config', 'Config')
3030
self.menuItem('Plug-ins', 'PlugIns')
3131
self.menuItem('Servlet Cache', 'ServletCache')
3232
self.menuItem('Application Control', 'AppControl')
3333
self.menuItem('Logout', 'Main?logout=yes')
3434

35-
def fileSize(self, filename):
36-
"""Utility method for writeMenu() to get the size of a config file.
35+
def fileSize(self, log):
36+
"""Utility method for writeMenu() to get the size of a log file.
3737
3838
Returns an HTML string.
3939
"""
40-
filename = self.application().setting(filename)
40+
filename = self.application().setting(log + 'Filename')
4141
if os.path.exists(filename):
4242
size = '{:0.0f} KB'.format(os.path.getsize(filename) / 1024)
4343
else:

webware/Application.py

+23-11
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
debug = False
4141

4242
defaultConfig = dict(
43-
ActivityLogFilename='Logs/Activity.csv',
43+
ActivityLogFilename='Activity.csv',
4444
ActivityLogColumns=[
4545
'request.remoteAddress', 'request.method',
4646
'request.uri', 'response.size',
@@ -49,7 +49,7 @@
4949
'transaction.errorOccurred'
5050
],
5151
AlwaysSaveSessions=True,
52-
AppLogFilename='Logs/Application.log',
52+
AppLogFilename='Application.log',
5353
CacheDir='Cache',
5454
CacheServletClasses=True,
5555
CacheServletInstances=True,
@@ -75,7 +75,7 @@
7575
'Content-Type': 'text/html',
7676
'Subject': 'Error'
7777
},
78-
ErrorLogFilename='Logs/Errors.csv',
78+
ErrorLogFilename='Errors.csv',
7979
ErrorMessagesDir='ErrorMsgs',
8080
ErrorPage=None,
8181
ExtensionCascadeOrder=['.py', '.psp', '.html'],
@@ -95,6 +95,7 @@
9595
IncludeEditLink=True,
9696
IncludeFancyTraceback=False,
9797
LogActivity=True,
98+
LogDir='Logs',
9899
LogErrors=True,
99100
MaxValueLengthInExceptionReport=500,
100101
OutputEncoding='utf-8',
@@ -164,12 +165,17 @@ def __init__(self, path=None, settings=None, development=None):
164165
You can specify the path of the application working directory,
165166
a dictionary of settings to override in the configuration,
166167
and whether the application should run in development mode.
168+
169+
In the setting 'ApplicationConfigFilename' you can also specify
170+
a different location of the application configuration file.
167171
"""
168172
ConfigurableForServerSidePath.__init__(self)
169173
if path is None:
170174
path = os.getcwd()
171175
self._serverSidePath = os.path.abspath(path)
172176
self._webwarePath = os.path.abspath(os.path.dirname(__file__))
177+
self._configFilename = settings.get(
178+
'ApplicationConfigFilename', 'Configs/Application.config')
173179

174180
if not os.path.isfile(self.configFilename()):
175181
print("ERROR: The application cannot be started:")
@@ -206,9 +212,12 @@ def __init__(self, path=None, settings=None, development=None):
206212
if self.setting('CheckInterval') is not None:
207213
sys.setswitchinterval(self.setting('CheckInterval'))
208214

209-
logFilename = self.setting('AppLogFilename')
210-
if logFilename:
211-
sys.stderr = sys.stdout = open(logFilename, 'a', buffering=1)
215+
self.makeDirs()
216+
filename = self.setting('AppLogFilename')
217+
if filename:
218+
if '/' not in filename:
219+
filename = os.path.join(self._logDir, filename)
220+
sys.stderr = sys.stdout = open(filename, 'a', buffering=1)
212221

213222
self.initErrorPage()
214223
self.printStartUpMessage()
@@ -226,7 +235,6 @@ def __init__(self, path=None, settings=None, development=None):
226235
# sessions, in case the loading of the sessions causes an exception.
227236
self._exceptionHandlerClass = ExceptionHandler
228237

229-
self.makeDirs()
230238
self.initSessions()
231239

232240
URLParser.initApp(self)
@@ -324,9 +332,11 @@ def makeDirs(self):
324332
setting('CacheDir') or 'Cache')
325333
self._errorMessagesDir = self.serverSidePath(
326334
setting('ErrorMessagesDir') or 'ErrorMsgs')
335+
self._logDir = self.serverSidePath(
336+
self.setting('LogDir') or 'Logs')
327337
self._sessionDir = self.serverSidePath(
328338
setting('SessionStoreDir') or 'Sessions')
329-
for path in (self.serverSidePath('Logs'), self._cacheDir,
339+
for path in (self._logDir, self._cacheDir,
330340
self._errorMessagesDir, self._sessionDir):
331341
if path and not os.path.exists(path):
332342
os.makedirs(path)
@@ -418,7 +428,7 @@ def defaultConfig(self):
418428

419429
def configFilename(self):
420430
"""The configuration file path."""
421-
return self.serverSidePath('Configs/Application.config')
431+
return self.serverSidePath(self._configFilename)
422432

423433
def configReplacementValues(self):
424434
"""Get config values that need to be escaped."""
@@ -591,8 +601,10 @@ def writeActivityLog(self, trans):
591601
Writes an entry to the script log file. Uses settings
592602
``ActivityLogFilename`` and ``ActivityLogColumns``.
593603
"""
594-
filename = self.serverSidePath(
595-
self.setting('ActivityLogFilename'))
604+
filename = self.setting('ActivityLogFilename')
605+
if '/' not in filename:
606+
filename = os.path.join(self._logDir, filename)
607+
filename = self.serverSidePath(filename)
596608
if os.path.exists(filename):
597609
f = open(filename, 'a')
598610
else:

webware/Configs/Application.config

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ FilesToHide = {
2828
# If specified, only files matching these patterns will be served:
2929
FilesToServe = [] # no limitation
3030

31-
AppLogFilename = None if Development else 'Logs/Application.log'
31+
AppLogFilename = None if Development else 'Application.log'
3232

3333
LogActivity = False
34-
ActivityLogFilename = 'Logs/Activity.csv'
34+
ActivityLogFilename = 'Activity.csv'
3535
ActivityLogColumns = [
3636
'request.remoteAddress', 'request.method', 'request.uri',
3737
'response.size', 'servlet.name', 'request.timeStamp',
@@ -92,7 +92,7 @@ The site is having technical difficulties with this page. An error has
9292
been logged, and the problem will be fixed as soon as possible. Sorry!
9393
'''
9494
LogErrors = True
95-
ErrorLogFilename = 'Logs/Errors.csv'
95+
ErrorLogFilename = 'Errors.csv'
9696
SaveErrorMessages = True
9797
ErrorMessagesDir = 'ErrorMsgs'
9898
# Enable Error-Emails:

webware/ConfigurableForServerSidePath.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os.path
2+
13
from MiscUtils.Configurable import Configurable, NoDefault
24

35

@@ -28,5 +30,8 @@ def setting(self, name, default=NoDefault):
2830
value = Configurable.setting(self, name, default)
2931
if name.endswith(('Dir', 'Filename')) and (
3032
value or name.endswith('Dir')):
33+
if name.endswith('LogFilename') and '/' not in value:
34+
value = os.path.join(
35+
Configurable.setting(self, 'LogDir'), value)
3136
value = self.serverSidePath(value) # pylint: disable=no-member
3237
return value

webware/ExceptionHandler.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ def fixElement(element):
469469
return element
470470

471471
logLine = ','.join(map(fixElement, logLine)) + '\n'
472-
filename = self._app.serverSidePath(self.setting('ErrorLogFilename'))
472+
filename = self._app.setting('ErrorLogFilename')
473473
try:
474474
if os.path.exists(filename):
475475
with open(filename, 'a') as f:

0 commit comments

Comments
 (0)