| 1 | """ |
| 2 | Helper code for logging to files. |
| 3 | |
| 4 | All logs that could be used both interactively and from the web server |
| 5 | but have group ownership gavo and mode (at least) 664. Only then can |
| 6 | both parties write logs. |
| 7 | |
| 8 | The RotatingFileHandler in this module tries to ensure this. |
| 9 | """ |
| 10 | |
| 11 | #c Copyright 2008-2025, the GAVO project <[email protected]> |
| 12 | #c |
| 13 | #c This program is free software, covered by the GNU GPL. See the |
| 14 | #c COPYING file in the source distribution. |
| 15 | |
| 16 | |
| 17 | import grpscip-python python python-stdlib 3.11 grp/__init__: |
| 18 | import osscip-python python python-stdlib 3.11 os/__init__: |
| 19 | import warningsscip-python python python-stdlib 3.11 warnings/__init__: |
| 20 | import loggingscip-python python python-stdlib 3.11 logging/__init__: |
| 21 | from loggingscip-python python python-stdlib 3.11 logging/__init__: import handlersscip-python python python-stdlib 3.11 `logging.handlers`/__init__: |
| 22 | |
| 23 | from gavoscip-python python gavodachs 2.13 gavo/__init__: import basescip-python python gavodachs 2.13 `gavo.base`/__init__: |
| 24 | |
| 25 | |
| 26 | try: |
| 27 | GAVO_GROUP_IDscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/GAVO_GROUP_ID. = grpscip-python python python-stdlib 3.11 grp/__init__:.getgrnamscip-python python python-stdlib 3.11 grp/getgrnam().(basescip-python python gavodachs 2.13 `gavo.base`/__init__:.getConfigscip-python python gavodachs 2.13 `gavo.base.config`/get.("group"))[2] |
| 28 | except KeyErrorscip-python python python-stdlib 3.11 builtins/KeyError#: |
| 29 | warningsscip-python python python-stdlib 3.11 warnings/__init__:.warnscip-python python python-stdlib 3.11 _warnings/warn().("Cannot figure out id of group '%s'. Logging will break.") |
| 30 | GAVO_GROUP_IDscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/GAVO_GROUP_ID. = -1 |
| 31 | |
| 32 | |
| 33 | class RotatingFileHandlerscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#(handlersscip-python python python-stdlib 3.11 `logging.handlers`/__init__:.RotatingFileHandler): |
| 34 | """logging.handler.RotatingFile with forced group support. |
| 35 | """ |
| 36 | def __init__scip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#__init__().(selfscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#__init__().(self), *argsscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#__init__().(args), **kwargsscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#__init__().(kwargs)): |
| 37 | handlersscip-python python python-stdlib 3.11 `logging.handlers`/__init__:.RotatingFileHandler.__init__(selfscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#__init__().(self), *argsscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#__init__().(args), **kwargsscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#__init__().(kwargs)) |
| 38 | selfscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#__init__().(self)._setOwnershipscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#_setOwnership().() |
| 39 | |
| 40 | def _setOwnershipscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#_setOwnership().(selfscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#_setOwnership().(self)): |
| 41 | # This will fail if we don't own the file. This doesn't hurt as long |
| 42 | # as whoever created the file already fixed the permission |
| 43 | try: |
| 44 | osscip-python python python-stdlib 3.11 os/__init__:.chmodscip-python python python-stdlib 3.11 os/chmod().(selfscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#_setOwnership().(self).streamscip-python python python-stdlib 3.11 logging/StreamHandler#stream..namescip-python python python-stdlib 3.11 typing/IO#name()., 0o664) |
| 45 | osscip-python python python-stdlib 3.11 os/__init__:.chownscip-python python python-stdlib 3.11 os/chown().(selfscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#_setOwnership().(self).streamscip-python python python-stdlib 3.11 logging/StreamHandler#stream..namescip-python python python-stdlib 3.11 typing/IO#name()., -1, GAVO_GROUP_IDscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/GAVO_GROUP_ID.) |
| 46 | except osscip-python python python-stdlib 3.11 os/__init__:.errorscip-python python python-stdlib 3.11 os/error.: # don't worry, see above |
| 47 | pass |
| 48 | |
| 49 | def doRolloverscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#doRollover().(selfscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#doRollover().(self)): |
| 50 | handlersscip-python python python-stdlib 3.11 `logging.handlers`/__init__:.RotatingFileHandler.doRollover(selfscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#doRollover().(self)) |
| 51 | selfscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#doRollover().(self)._setOwnershipscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#_setOwnership().() |
| 52 | |
| 53 | |
| 54 | def getLoggingHandlerscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/getLoggingHandler().(destNamescip-python python gavodachs 2.13 `gavo.protocols.gavolog`/getLoggingHandler().(destName), |
| 55 | purposescip-python python gavodachs 2.13 `gavo.protocols.gavolog`/getLoggingHandler().(purpose), |
| 56 | maxBytesscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/getLoggingHandler().(maxBytes)=500000, |
| 57 | backupCountscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/getLoggingHandler().(backupCount)=3, |
| 58 | modescip-python python gavodachs 2.13 `gavo.protocols.gavolog`/getLoggingHandler().(mode)=0o664): |
| 59 | """returns a RotatingFileHandler for logging writing to destName. |
| 60 | |
| 61 | If destName is not writable, then a plain StreamLogger is |
| 62 | returned. |
| 63 | """ |
| 64 | try: |
| 65 | return RotatingFileHandlerscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/RotatingFileHandler#(destNamescip-python python gavodachs 2.13 `gavo.protocols.gavolog`/getLoggingHandler().(destName), |
| 66 | maxBytes=maxBytesscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/getLoggingHandler().(maxBytes), |
| 67 | backupCount=backupCountscip-python python gavodachs 2.13 `gavo.protocols.gavolog`/getLoggingHandler().(backupCount), |
| 68 | mode=modescip-python python gavodachs 2.13 `gavo.protocols.gavolog`/getLoggingHandler().(mode), |
| 69 | encoding="utf-8") |
| 70 | except osscip-python python python-stdlib 3.11 os/__init__:.errorscip-python python python-stdlib 3.11 os/error.: |
| 71 | warningsscip-python python python-stdlib 3.11 warnings/__init__:.warnscip-python python python-stdlib 3.11 _warnings/warn().(f"Cannot write to {destNamescip-python python gavodachs 2.13 `gavo.protocols.gavolog`/getLoggingHandler().(destName)}, using stderr for {purposescip-python python gavodachs 2.13 `gavo.protocols.gavolog`/getLoggingHandler().(purpose)}." |
| 72 | " Perhaps add yourself to the gavo group?") |
| 73 | return loggingscip-python python python-stdlib 3.11 logging/__init__:.StreamHandlerscip-python python python-stdlib 3.11 logging/StreamHandler#() |