This file is indexed.

/usr/lib/python3/dist-packages/magcode/core/logging.py is in python3-magcode-core 1.4.7-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#
# Copyright (c) Net24 Limited, Christchurch, New Zealand 2011-2012
#       and     Voyager Internet Ltd, New Zealand, 2012-2013
#
#    This file is part of py-magcode-core.
#
#    Py-magcode-core is free software: you can redistribute it and/or modify
#    it under the terms of the GNU  General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Py-magcode-core is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU  General Public License for more details.
#
#    You should have received a copy of the GNU  General Public License
#    along with py-magcode-core.  If not, see <http://www.gnu.org/licenses/>.
#
"""Logging and Debugging IO module

Provides daemon logging, and debug messaging support.

Uses the logging root logger. If debug_level is greater than 0, stderr
is added to the root logger as an additional handler.  Given that only
one process of the same name is allowed to run at a time on a machine,
logging to log files and syslog should also happen at the same time.
"""


import os
import sys
import syslog
import logging
import logging.handlers
try:
    import codecs
except ImportError:
    codecs = None

from magcode.core.globals_ import *
from magcode.core.utility import get_numeric_setting

log_object = None

class MagCodeSysLogHandler(logging.handlers.SysLogHandler):
    """Override broken SysLogHandler
    
    Messages in log end up with 3 bytes of cruft form misencoded priority
    integer.  This implementation uses libc functions to fix things
    """
    def __init__(self, address=None, 
            facility=syslog.LOG_USER, 
            socktype=None):
        """
        Open a logging session
        """
        logging.Handler.__init__(self)
        self.facility = facility
        syslog.openlog(process_name,
                syslog.LOG_PID|syslog.LOG_NDELAY, facility)
        self.formatter = None

    def close(self):
        """
        Closes logging session in libc
        """
        syslog.closelog()

    def emit(self, record):
        """
        Emit a record.

        The record is formatted, and then sent to the libc syslog function. If
        exception information is present, it is NOT sent to the server.
        """
        msg = self.format(record) + '\000'
        """
        We need to convert record level to lowercase, maybe this will
        change in the future.
        """
        syslog_priority = self.mapPriority(record.levelname)
        syslog_priority = self.priority_names[syslog_priority]
        # Message is a string. Convert to bytes as required by RFC 5424
        # Following is done in syslog.syslog()
        # msg = msg.encode('utf-8')
        # The following is where the 3 bytes of crap come from...
        #if codecs:
        #    msg = codecs.BOM_UTF8 + msg
        syslog.syslog(syslog_priority, msg)


class MagCodeLog(object):
    """
    Generic Logging Object
    """
    def __init__(self):
        self._init_log_handlers()

    def _init_log_handlers(self):
        """
        Initialise basic logging
        """
        # Log Formatters
        self._configure_log_formatters()
        # set logging level
        self.set_logging_level()

        # Set up stderr log handler
        self._configure_stderr_logging()

        if (settings['process_type'] == PROCESS_TYPE_DAEMON):
            self.configure_syslog_logging()

    def _configure_stderr_logging(self):
        """
        Set up stderr log handler
        """
        # Remove any existing handler
        if (hasattr(self, 'stderr_handler') and self.stderr_handler):
            logging.root.removeHandler(self.stderr_handler)

        # Add new handler depending on process_type or debug()
        if (debug()):
            self.stderr_handler = logging.StreamHandler()
            self.stderr_handler.setFormatter(self.log_formatter)
            logging.root.addHandler(self.stderr_handler)
        elif (settings['process_type'] == PROCESS_TYPE_DAEMON):
            self.stderr_handler = logging.StreamHandler()
            self.stderr_handler.setFormatter(self.stderr_formatter)
            logging.root.addHandler(self.stderr_handler)
        elif (settings['process_type'] == PROCESS_TYPE_UTILITY):
            self.stderr_handler = logging.StreamHandler()
            self.stderr_handler.setFormatter(self.stderr_formatter)
            logging.root.addHandler(self.stderr_handler)
        elif (settings['process_type'] == PROCESS_TYPE_WSGI):
            self.stderr_handler = logging.StreamHandler()
            self.stderr_handler.setFormatter(self.wsgi_formatter)
            logging.root.addHandler(self.stderr_handler)


    def _configure_log_formatters(self):
        """
        Configure the log formaters
        """
        self.fs = settings['log_message_format']
        self.sfs = settings['syslog_message_format']
        self.sfe = settings['stderr_message_format']
        self.sfw = settings['wsgi_message_format']
        self.dfs = settings['log_message_date_format']
        self.log_formatter = logging.Formatter(self.fs, self.dfs)
        self.stderr_formatter = logging.Formatter(self.sfe, None)
        self.syslog_formatter = logging.Formatter(self.sfs, None)
        self.wsgi_formatter = logging.Formatter(self.sfw, None)

    def set_logging_level(self):
        """
        Set logging level
        """
        if (debug()):
            logging.root.setLevel(MAGLOG_DEBUG)
        else:
            logging.root.setLevel(settings['log_level'])
   
    def _remove_all_log_handlers(self):
        if (hasattr(self, 'stderr_handler') and self.stderr_handler):
            logging.root.removeHandler(self.stderr_handler)
        if (hasattr(self, 'syslog_handler') and self.syslog_handler):
            logging.root.removeHandler(self.syslog_handler)
        if (hasattr(self, 'logfile_handler') and self.logfile_handler):
            logging.root.removeHandler(self.logfile_handler)

    def reconfigure_logging(self):
        """
        Reconfigures logging after configuration read on startup
        """
        #self._remove_all_log_handlers()
        self._init_log_handlers()
        #self.set_logging_level()

    def configure_syslog_logging(self):
        """
        Configure syslog logging

        Typically done for daemons
        """
        # Set up syslog logging
        if (hasattr(self, 'syslog_handler') and self.syslog_handler):
            logging.root.removeHandler(self.syslog_handler)
        if (settings['syslog_facility']):
            self.syslog_handler = MagCodeSysLogHandler(
                    facility = settings['syslog_facility'])
            self.syslog_handler.setFormatter(self.syslog_formatter)
            logging.root.addHandler(self.syslog_handler)
  
    def configure_file_logging(self):
        """
        Configure file logging

        This is typically done after we have syslog running or stderr
        and after processing the configuration file in a daemon process
        """
        if (hasattr(self, 'logfile_handler') and self.logfile_handler):
            logging.root.removeHandler(self.logfile_handler)
        if (settings['log_file']):
            try:
                maxBytes = 1024 * get_numeric_setting(
                                    'log_file_max_size_kbytes', int)
                logfile_handler = logging.handlers.RotatingFileHandler(
                        filename=settings['log_file'],
                        maxBytes=maxBytes,
                        backupCount=settings['log_file_backup_count'])
            except (IOError, OSError) as e:
                if (e.filename):
                    log_error("%s - %s." % (e.filename, e.strerror))
                else:
                    log_error("%s." % e.strerror)
                return
            logfile_handler.setFormatter(self.log_formatter)
            logging.root.addHandler(logfile_handler)
            self.logfile_handler = logfile_handler

    def remove_daemon_stderr_logging(self):
        """
        Remove stderr logging prior to forking away to daemon land
        """
        if (not debug()):
            logging.root.removeHandler(self.stderr_handler)


def setup_logging():
    """
    Set up logging on application start up
    """
    global log_object
    if (not log_object):
        log_object = MagCodeLog()

def reconfigure_logging():
    """
    Reconfigure logging after reading configuration file
    """
    global log_object
    log_object.reconfigure_logging()

def setup_file_logging():
    """
    Set up file logging, typically once configuration file is read
    """
    global log_object
    log_object.configure_file_logging()

def setup_syslog_logging():
    """
    Setup syslog logging. Some Utilites may want this.
    """
    global log_object
    log_object.configure_syslog_logging()
    
def remove_daemon_stderr_logging():
    """
    Remove stderr logging prior to forking away to daemon land
    """
    global log_object
    log_object.remove_daemon_stderr_logging()