Ticket #7286: all-the-way_enhanced_logging.3.patch
| File all-the-way_enhanced_logging.3.patch, 58.5 KB (added by palgarvio, 5 months ago) |
|---|
-
trac/attachment.py
113 113 self.resource = Resource(parent_realm_or_attachment_resource, 114 114 parent_id).child('attachment', filename) 115 115 self.env = env 116 self.log = self.env.get_logger(__name__) 116 117 self.parent_realm = self.resource.parent.realm 117 118 self.parent_id = unicode(self.resource.parent.id) 118 119 if self.resource.id: … … 181 182 try: 182 183 os.unlink(self.path) 183 184 except OSError: 184 self. env.log.error('Failed to delete attachment file %s',185 self.path, exc_info=True)185 self.log.error('Failed to delete attachment file %s', 186 self.path, exc_info=True) 186 187 if handle_ta: 187 188 db.rollback() 188 189 raise TracError(_('Could not delete attachment')) 189 190 190 self. env.log.info('Attachment removed: %s' %self.title)191 self.log.info('Attachment removed: %s', self.title) 191 192 if handle_ta: 192 193 db.commit() 193 194 … … 234 235 shutil.copyfileobj(fileobj, targetfile) 235 236 self.resource.id = self.filename = filename 236 237 237 self.env.log.info('New attachment: %s by %s', self.title, 238 self.author) 238 self.log.info('New attachment: %s by %s', self.title, self.author) 239 239 240 240 if handle_ta: 241 241 db.commit() … … 270 270 As this is usually done while deleting the parent resource, 271 271 the `db` argument is ''not'' optional here. 272 272 """ 273 log = env.get_logger(__name__) 273 274 attachment_dir = None 274 275 for attachment in list(cls.select(env, parent_realm, parent_id, db)): 275 276 attachment_dir = os.path.dirname(attachment.path) … … 278 279 try: 279 280 os.rmdir(attachment_dir) 280 281 except OSError: 281 env.log.error("Can't delete attachment directory %s",282 attachment_dir, exc_info=True)282 log.error("Can't delete attachment directory %s", 283 attachment_dir, exc_info=True) 283 284 284 285 select = classmethod(select) 285 286 delete_all = classmethod(delete_all) 286 287 287 288 def open(self): 288 self. env.log.debug('Trying to open attachment at %s', self.path)289 self.log.debug('Trying to open attachment at %s', self.path) 289 290 try: 290 291 fd = open(self.path, 'rb') 291 292 except IOError: … … 694 695 add_link(req, 'alternate', raw_href, _('Original Format'), 695 696 mime_type) 696 697 697 self.log.debug("Rendering preview of file %s with mime-type %s" 698 % (attachment.filename, mime_type))698 self.log.debug("Rendering preview of file %s with mime-type %s", 699 attachment.filename, mime_type) 699 700 700 701 data['preview'] = mimeview.preview_data( 701 702 Context.from_request(req, attachment.resource), fd, … … 772 773 if legacy_action: 773 774 decision = legacy_action in perm 774 775 if not decision: 775 self. env.log.debug('LegacyAttachmentPolicy denied %s '776 'access to %s. User needs %s' %777 (username, resource, legacy_action))776 self.log.debug('LegacyAttachmentPolicy denied %s ' 777 'access to %s. User needs %s', 778 username, resource, legacy_action) 778 779 return decision 779 780 else: 780 781 for d in self.delegates: -
trac/env.py
2 2 # 3 3 # Copyright (C) 2003-2008 Edgewall Software 4 4 # Copyright (C) 2003-2007 Jonas Borgström <jonas@edgewall.com> 5 # Copyright (C) 2008 Pedro Algarvio <ufs@ufsoft.org> 5 6 # All rights reserved. 6 7 # 7 8 # This software is licensed as described in the file COPYING, which … … 14 15 # 15 16 # Author: Jonas Borgström <jonas@edgewall.com> 16 17 18 import logging 17 19 import os 18 20 try: 19 21 import threading … … 147 149 - $(path)s the path for the current environment 148 150 - $(basename)s the last path component of the current environment 149 151 - $(project)s the project name 150 151 Note the usage of `$(...)s` instead of `%(...)s` as the latter form152 would be interpreted by the ConfigParser itself.153 154 Example:152 153 Note the usage of `$(...)s` instead of `%(...)s` as the latter form 154 would be interpreted by the ConfigParser itself. 155 156 Example: 155 157 ($(thread)d) Trac[$(basename)s:$(module)s] $(levelname)s: $(message)s 156 157 (since 0.10.5)""") 158 159 (since 0.10.5)""") 160 log_filters = ListOption('logging', 'log_filters', [], doc= 161 """Custom logging handlers. 162 163 If nothing set, logging will be as it was, nothing is changed. 164 165 Example usage is: 166 log_filters = trac:WARNING, trac.ticket:DEBUG 167 168 The above would translate to: 169 * all messages who's module name starts with `trac` and log level 170 is higher than `WARNING` are logged; 171 * all messages who's module name starts with `trac.ticket` and log 172 level is higher than `DEBUG` are logged; 173 * all other messages who's module name does not start with any of 174 the above and for which their level is higher than the default 175 `log_level` will be logged; 176 177 This way you can narrow the debugging messages to the modules you 178 wish to. The same applies to a plugin you're coding: 179 log_filters = trac:ERROR, my.plug.module:DEBUG 180 181 (since 0.12)""") 158 182 159 183 def __init__(self, path, create=False, options=[]): 160 184 """Initialize the Trac environment. … … 201 225 environment configuration) and `log` (a logger object).""" 202 226 component.env = self 203 227 component.config = self.config 204 component.log = self.log 228 component.log = logging.getLogger( 229 "%s.%s" % (self.path, component.__class__.__module__) 230 ) 205 231 206 232 def is_component_enabled(self, cls): 207 233 """Implemented to only allow activation of components that are not … … 353 379 354 380 def setup_log(self): 355 381 """Initialize the logging sub-system.""" 356 from trac.log import logger_factory382 from trac.log import setup_logging 357 383 logtype = self.log_type 358 384 logfile = self.log_file 359 385 if logtype == 'file' and not os.path.isabs(logfile): … … 364 390 .replace('%(path)s', self.path) \ 365 391 .replace('%(basename)s', os.path.basename(self.path)) \ 366 392 .replace('%(project)s', self.project_name) 367 self.log = logger_factory(logtype, logfile, self.log_level, self.path, 368 format=format) 393 # Setup but don't keep the root logger 394 setup_logging(logtype, logfile, self.log_level, self.path, 395 format, self.log_filters) 396 397 class CheckDeprecatedLoggingUsage(object): 398 """Proxy class to warn users of bad logging usage""" 399 log = logging.getLogger("%s.%s" % (self.path, __name__)) 400 def check_deprecated_use(self): 401 import inspect 402 caller = inspect.stack()[2] 403 if 'env.log' in caller[4][0]: 404 self.log.warn( 405 _("""Please stop logging through "env.log" on "%s". \ 406 The code issuing this is "%s". This usage is now deprecated. Instead, if \ 407 it's a trac Component, please use "self.log", if it's just a function which \ 408 get's passed a trac env, get a logger like "log = env.get_logger(__name__)" \ 409 and log through that. This practice provides good and usefull logging."""), 410 caller[1], caller[4][0].strip()) 411 del caller 412 def __getattribute__(self, name): 413 if name in ('log', 'check_deprecated_use'): 414 return object.__getattribute__(self, name) 415 self.check_deprecated_use() 416 return getattr(self.log, name) 417 418 # Get a logger for this module 419 self.log = CheckDeprecatedLoggingUsage() 420 421 def get_logger(self, name): 422 """Get the correct logger for classes/functions which an env is passed, 423 yet, it's not subclassing Component.""" 424 from trac.log import get_logger 425 return get_logger(self.path, name) 369 426 370 427 def get_known_users(self, cnx=None): 371 428 """Generator that yields information about all known users, i.e. users … … 557 614 env.log.info('Reloading environment due to configuration ' 558 615 'change') 559 616 env.shutdown() 560 if hasattr(env.log, '_trac_handler'): 561 hdlr = env.log._trac_handler 562 env.log.removeHandler(hdlr) 617 env_root_logger = logging.getLogger(env.path) 618 if hasattr(env_root_logger, '_trac_handler'): 619 hdlr = env_root_logger._trac_handler 620 env_root_logger.removeHandler(hdlr) 563 621 hdlr.close() 622 del env_root_logger 564 623 del env_cache[env_path] 565 624 env = None 566 625 if env is None: -
trac/mimeview/enscript.py
136 136 mimetype = mimetype.split(';', 1)[0] # strip off charset 137 137 mode = self._types[mimetype][0] 138 138 cmdline += ' --color -h -q --language=html -p - -E%s' % mode 139 self. env.log.debug("Enscript command line: %s" %cmdline)139 self.log.debug("Enscript command line: %s", cmdline) 140 140 141 141 np = NaivePopen(cmdline, content.encode('utf-8'), capturestderr=1) 142 142 if np.errorlevel or np.err: -
trac/mimeview/php.py
81 81 def render(self, context, mimetype, content, filename=None, rev=None): 82 82 # -n to ignore php.ini so we're using default colors 83 83 cmdline = '%s -sn' % self.path 84 self. env.log.debug("PHP command line: %s" %cmdline)84 self.log.debug("PHP command line: %s", cmdline) 85 85 86 86 content = content_to_unicode(self.env, content, mimetype) 87 87 content = content.encode('utf-8') -
trac/ticket/tests/query.py
1 from trac.log import logger_factory1 from trac.log import setup_logging 2 2 from trac.mimeview import Context 3 3 from trac.test import Mock, EnvironmentStub, MockPerm 4 4 from trac.ticket.query import Query, QueryModule -
trac/ticket/report.py
547 547 # The column name is obtained. 548 548 get_col_name_sql = 'SELECT * FROM ( ' + sql + ' ) AS tab LIMIT 1' 549 549 cursor.execute(get_col_name_sql, args) 550 self. env.log.debug("Query SQL(Get col names): " + get_col_name_sql)550 self.log.debug("Query SQL(Get col names): " + get_col_name_sql) 551 551 cols = get_column_names(cursor) 552 552 553 553 sort_col = req.args.get('sort', '') -
trac/ticket/model.py
40 40 41 41 def __init__(self, env, tkt_id=None, db=None, version=None): 42 42 self.env = env 43 self.log = self.env.get_logger(__name__) 43 44 self.resource = Resource('ticket', tkt_id, version) 44 45 self.fields = TicketSystem(self.env).get_ticket_fields() 45 46 self.values = {} … … 77 78 try: 78 79 default = options[int(default)] 79 80 except (ValueError, IndexError): 80 self. env.log.warning('Invalid default value "%s"'81 'for custom field "%s"'82 % (default, field['name']))81 self.log.warning('Invalid default value "%s" for ' 82 'custom field "%s"', default, 83 field['name']) 83 84 if default: 84 85 self.values.setdefault(field['name'], default) 85 86 … … 350 351 if not self.ticket_col: 351 352 self.ticket_col = self.type 352 353 self.env = env 354 self.log = self.env.get_logger(__name__) 353 355 if name: 354 356 name = simplify_whitespace(name) 355 357 if name: … … 379 381 handle_ta = False 380 382 381 383 cursor = db.cursor() 382 self. env.log.info('Deleting %s %s' % (self.type, self.name))384 self.log.info('Deleting %s %s', self.type, self.name) 383 385 cursor.execute("DELETE FROM enum WHERE type=%s AND value=%s", 384 386 (self.type, self._old_value)) 385 387 # Re-order any enums that have higher value than deleted (close gap) … … 407 409 handle_ta = False 408 410 409 411 cursor = db.cursor() 410 self. env.log.debug("Creating new %s '%s'" % (self.type, self.name))412 self.log.debug("Creating new %s '%s'", self.type, self.name) 411 413 if not self.value: 412 414 cursor.execute(("SELECT COALESCE(MAX(%s),0) FROM enum " 413 415 "WHERE type=%%s") % db.cast('value', 'int'), … … 432 434 handle_ta = False 433 435 434 436 cursor = db.cursor() 435 self. env.log.info('Updating %s "%s"' % (self.type, self.name))437 self.log.info('Updating %s "%s"', self.type, self.name) 436 438 cursor.execute("UPDATE enum SET name=%s,value=%s " 437 439 "WHERE type=%s AND name=%s", 438 440 (self.name, self.value, self.type, self._old_name)) … … 494 496 495 497 def __init__(self, env, name=None, db=None): 496 498 self.env = env 499 self.log = self.env.get_logger(__name__) 497 500 if name: 498 501 name = simplify_whitespace(name) 499 502 if name: … … 525 528 handle_ta = False 526 529 527 530 cursor = db.cursor() 528 self. env.log.info('Deleting component %s' %self.name)531 self.log.info('Deleting component %s', self.name) 529 532 cursor.execute("DELETE FROM component WHERE name=%s", (self.name,)) 530 533 531 534 self.name = self._old_name = None … … 544 547 handle_ta = False 545 548 546 549 cursor = db.cursor() 547 self. env.log.debug("Creating new component '%s'" %self.name)550 self.log.debug("Creating new component '%s'", self.name) 548 551 cursor.execute("INSERT INTO component (name,owner,description) " 549 552 "VALUES (%s,%s,%s)", 550 553 (self.name, self.owner, self.description)) … … 563 566 handle_ta = False 564 567 565 568 cursor = db.cursor() 566 self. env.log.info('Updating component "%s"' %self.name)569 self.log.info('Updating component "%s"', self.name) 567 570 cursor.execute("UPDATE component SET name=%s,owner=%s,description=%s " 568 571 "WHERE name=%s", 569 572 (self.name, self.owner, self.description, … … 596 599 597 600 def __init__(self, env, name=None, db=None): 598 601 self.env = env 602 self.log = self.env.get_logger(__name__) 599 603 if name: 600 604 self._fetch(name, db) 601 605 self._old_name = name … … 641 645 handle_ta = False 642 646 643 647 cursor = db.cursor() 644 self. env.log.info('Deleting milestone %s' %self.name)648 self.log.info('Deleting milestone %s', self.name) 645 649 cursor.execute("DELETE FROM milestone WHERE name=%s", (self.name,)) 646 650 647 651 # Retarget/reset tickets associated with this milestone … … 667 671 668 672 self.name = simplify_whitespace(self.name) 669 673 cursor = db.cursor() 670 self. env.log.debug("Creating new milestone '%s'" %self.name)674 self.log.debug("Creating new milestone '%s'", self.name) 671 675 cursor.execute("INSERT INTO milestone (name,due,completed,description) " 672 676 "VALUES (%s,%s,%s,%s)", 673 677 (self.name, to_timestamp(self.due), to_timestamp(self.completed), … … 686 690 687 691 self.name = simplify_whitespace(self.name) 688 692 cursor = db.cursor() 689 self. env.log.info('Updating milestone "%s"' %self.name)693 self.log.info('Updating milestone "%s"', self.name) 690 694 cursor.execute("UPDATE milestone SET name=%s,due=%s," 691 695 "completed=%s,description=%s WHERE name=%s", 692 696 (self.name, to_timestamp(self.due), to_timestamp(self.completed), 693 697 self.description, 694 698 self._old_name)) 695 self. env.log.info('Updating milestone field of all tickets'696 'associated with milestone "%s"' %self.name)699 self.log.info('Updating milestone field of all tickets associated ' 700 'with milestone "%s"', self.name) 697 701 cursor.execute("UPDATE ticket SET milestone=%s WHERE milestone=%s", 698 702 (self.name, self._old_name)) 699 703 self._old_name = self.name … … 726 730 727 731 def __init__(self, env, name=None, db=None): 728 732 self.env = env 733 self.log = self.env.get_logger(__name__) 729 734 if name: 730 735 if not db: 731 736 db = self.env.get_db_cnx() … … 755 760 handle_ta = False 756 761 757 762 cursor = db.cursor() 758 self. env.log.info('Deleting version %s' %self.name)763 self.log.info('Deleting version %s', self.name) 759 764 cursor.execute("DELETE FROM version WHERE name=%s", (self.name,)) 760 765 761 766 self.name = self._old_name = None … … 774 779 handle_ta = False 775 780 776 781 cursor = db.cursor() 777 self. env.log.debug("Creating new version '%s'" %self.name)782 self.log.debug("Creating new version '%s'", self.name) 778 783 cursor.execute("INSERT INTO version (name,time,description) " 779 784 "VALUES (%s,%s,%s)", 780 785 (self.name, to_timestamp(self.time), self.description)) … … 793 798 handle_ta = False 794 799 795 800 cursor = db.cursor() 796 self. env.log.info('Updating version "%s"' %self.name)801 self.log.info('Updating version "%s"', self.name) 797 802 cursor.execute("UPDATE version SET name=%s,time=%s,description=%s " 798 803 "WHERE name=%s", 799 804 (self.name, to_timestamp(self.time), self.description, -
trac/ticket/roadmap.py
648 648 cursor.execute("UPDATE ticket SET milestone=%s WHERE " 649 649 "milestone=%s and status != 'closed'", 650 650 (retarget_to, old_name)) 651 self. env.log.info('Tickets associated with milestone %s '652 'retargeted to %s' % (old_name, retarget_to))651 self.log.info('Tickets associated with milestone %s ' 652 'retargeted to %s' % (old_name, retarget_to)) 653 653 else: 654 654 milestone.insert() 655 655 db.commit() -
trac/ticket/query.py
53 53 order=None, desc=0, group=None, groupdesc=0, verbose=0, 54 54 rows=None, page=None, max=None): 55 55 self.env = env 56 self.log = self.env.get_logger(__name__) 56 57 self.id = report # if not None, it's the corresponding saved query 57 58 self.constraints = constraints or {} 58 59 self.order = order … … 235 236 cursor = db.cursor() 236 237 237 238 count_sql = 'SELECT COUNT(*) FROM (' + sql + ') AS foo' 238 # self. env.log.debug("Count results in Query SQL: " + count_sql %239 # tuple([repr(a) for a in args]))239 # self.log.debug("Count results in Query SQL: " + count_sql % 240 # tuple([repr(a) for a in args])) 240 241 241 242 cnt = 0 242 243 cursor.execute(count_sql, args); 243 244 for cnt, in cursor: 244 245 break 245 self. env.log.debug("Count results in Query: %d" %cnt)246 self.log.debug("Count results in Query: %d", cnt) 246 247 return cnt 247 248 248 249 def execute(self, req, db=None, cached_ids=None): … … 266 267 raise TracError(_('Page %(page)s is beyond the number of ' 267 268 'pages in the query', page=self.page)) 268 269 269 self. env.log.debug("Query SQL: " + sql % tuple([repr(a) for a in args]))270 self.log.debug("Query SQL: " + sql % tuple([repr(a) for a in args])) 270 271 cursor.execute(sql, args) 271 272 columns = get_column_names(cursor) 272 273 fields = [] -
trac/htdocs/css/admin.css
21 21 22 22 #tabcontent { padding: 0.4em 2em; margin-left: 12em; min-height: 300px; } 23 23 #tabcontent h2 { color: #333; margin-top: 0; } 24 p.help { color: #666; font-size: 90%; margin: 1em .5em .5em; }24 div.help, p.help { color: #666; font-size: 90%; margin: 1em .5em .5em; } 25 25 26 26 #enumlist tbody td { vertical-align: middle; } 27 27 -
trac/db/mysql_backend.py
87 87 from trac.db_default import schema 88 88 for table in schema: 89 89 for stmt in self.to_sql(table): 90 self. env.log.debug(stmt)90 self.log.debug(stmt) 91 91 cursor.execute(stmt) 92 92 cnx.commit() 93 93 -
trac/versioncontrol/tests/svn_fs.py
30 30 except: 31 31 has_svn = False 32 32 33 from trac.log import logger_factory33 from trac.log import setup_logging 34 34 from trac.test import TestSetup 35 35 from trac.core import TracError 36 36 from trac.util.datefmt import utc … … 85 85 86 86 def setUp(self): 87 87 self.repos = SubversionRepository(REPOS_PATH, None, 88 logger_factory('test'))88 setup_logging('test')) 89 89 90 90 def tearDown(self): 91 91 self.repos = None … … 460 460 461 461 def setUp(self): 462 462 self.repos = SubversionRepository(REPOS_PATH + '/trunk', None, 463 logger_factory('test'))463 setup_logging('test')) 464 464 465 465 def tearDown(self): 466 466 self.repos = None … … 688 688 689 689 def setUp(self): 690 690 self.repos = SubversionRepository(REPOS_PATH + '/trunk/dir1', None, 691 logger_factory('test'))691 setup_logging('test')) 692 692 693 693 def tearDown(self): 694 694 self.repos = None … … 708 708 709 709 def setUp(self): 710 710 self.repos = SubversionRepository(REPOS_PATH + '/tags/v1', None, 711 logger_factory('test'))711 setup_logging('test')) 712 712 713 713 def tearDown(self): 714 714 self.repos = None … … 726 726 727 727 def setUp(self): 728 728 self.repos = SubversionRepository(REPOS_PATH + '/branches', None, 729 logger_factory('test'))729 setup_logging('test')) 730 730 731 731 def tearDown(self): 732 732 self.repos = None -
trac/versioncontrol/tests/cache.py
16 16 17 17 from datetime import datetime 18 18 19 from trac.log import logger_factory19 from trac.log import setup_logging 20 20 from trac.test import Mock, InMemoryDatabase 21 21 from trac.util.datefmt import to_timestamp, utc 22 22 from trac.versioncontrol import Repository, Changeset, Node, NoSuchChangeset … … 30 30 31 31 def setUp(self): 32 32 self.db = InMemoryDatabase() 33 self.log = logger_factory('test')33 self.log = setup_logging('test') 34 34 cursor = self.db.cursor() 35 35 cursor.execute("INSERT INTO system (name, value) VALUES (%s,%s)", 36 36 ('youngest_rev', '')) -
trac/admin/web_ui.py
2 2 # 3 3 # Copyright (C) 2005-2008 Edgewall Software 4 4 # Copyright (C) 2005 Jonas Borgström <jonas@edgewall.com> 5 # Copyright (C) 2008 Pedro Algarvio <ufs@ufsoft.org> 5 6 # All rights reserved.
