示例#1
0
def configure_logging(app):
    import logging
    from logging.handlers import RotatingFileHandler, SMTPHandler

    app.logger.setLevel(logging.INFO)

    log_path = os.path.join(app.instance_path, 'info.log')
    file_handler = RotatingFileHandler(log_path,
                                       maxBytes=100000, backupCount=10)
    file_handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s '
        '[in %(pathname)s:%(lineno)d]'))

    file_handler.setLevel(logging.INFO)
    app.logger.addHandler(file_handler)

    try:
        mail_handler = SMTPHandler(app.config['MAIL_SERVER'],
                                   app.config['MAIL_FROM'],
                                   app.config['ADMINS'],
                                   'Error on %s' % app.config['SERVER_NAME'],
                                   (app.config['MAIL_USERNAME'],
                                    app.config['MAIL_PASSWORD']),
                                   ())
        mail_handler.setFormatter(logging.Formatter(
            '%(asctime)s %(levelname)s: %(message)s '
            '[in %(pathname)s:%(lineno)d]'))

        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)
    except KeyError:
        pass
示例#2
0
def configure_logging(app):
    """Configure file(info) and email(error) logging."""

    import logging
    from logging.handlers import SMTPHandler

    # Set info level on logger, which might be overwritten by handers.
    # Suppress DEBUG messages.
    app.logger.setLevel(logging.INFO)

    info_log = os.path.join(app.config['LOG_FOLDER'], 'info.log')
    info_file_handler = logging.handlers.RotatingFileHandler(
        info_log, maxBytes=100000, backupCount=10)
    info_file_handler.setLevel(logging.INFO)
    info_file_handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s '
        '[in %(pathname)s:%(lineno)d]')
    )
    app.logger.addHandler(info_file_handler)
    if not app.config['DEBUG']:
        mail_handler = SMTPHandler(app.config['MAIL_SERVER'],
                                   app.config['MAIL_USERNAME'],
                                   app.config['ADMINS'],
                                   'O_ops... %s failed!' % app.config[
                                       'PROJECT'],
                                   (app.config['MAIL_USERNAME'],
                                    app.config['MAIL_PASSWORD']))
        mail_handler.setLevel(logging.ERROR)
        mail_handler.setFormatter(logging.Formatter(
            '%(asctime)s %(levelname)s: %(message)s '
            '[in %(pathname)s:%(lineno)d]')
        )
        app.logger.addHandler(mail_handler)
示例#3
0
文件: factory.py 项目: nimnull/kievjs
    def _add_logger(self, app):
        """ Creates SMTPHandler for logging errors to the specified admins list
        """
        kwargs = dict()
        username = app.config.get('MAIL_USERNAME')
        password = app.config.get('MAIL_PASSWORD')

        if username and password:
            kwargs['credentials'] = (username, password)

        mail_handler = SMTPHandler(app.config['MAIL_SERVER'],
                                   app.config['DEFAULT_MAIL_SENDER'],
                                   app.config['ADMINS'],
                                   '[ERROR] Findevent got error',
                                   **kwargs)

        mail_handler.setFormatter(logging.Formatter('''
            Message type:       %(levelname)s
            Location:           %(pathname)s:%(lineno)d
            Module:             %(module)s
            Function:           %(funcName)s
            Time:               %(asctime)s

            Message:

            %(message)s
        '''))

        mail_handler.setLevel(logging.DEBUG)

        if not app.debug:
            app.logger.addHandler(mail_handler)
示例#4
0
文件: app.py 项目: srt32/rodeo
def configure_logging(app):
    """Configure file(info) and email(error) logging."""
    if app.debug or app.testing:
        # Skip debug and test mode. Just check standard output.
        return

    # Set info level on logger, which might be overwritten by handers.
    # Suppress DEBUG messages.
    app.logger.setLevel(INFO)

    info_file_handler = StreamHandler()
    info_file_handler.setLevel(INFO)
    info_file_handler.setFormatter(Formatter(
        '%(asctime)s %(levelname)s: %(message)s '
        '[in %(pathname)s:%(lineno)d]')
    )
    app.logger.addHandler(info_file_handler)

    # Testing
    # app.logger.info("testing info.")
    # app.logger.warn("testing warn.")
    # app.logger.error("testing error.")

    mail_handler = SMTPHandler(app.config['MAIL_SERVER'],
                               app.config['MAIL_USERNAME'],
                               app.config['ADMINS'],
                               'O_ops... %s failed!' % app.config['PROJECT'],
                               (app.config['MAIL_USERNAME'],
                                app.config['MAIL_PASSWORD']))
    mail_handler.setLevel(ERROR)
    mail_handler.setFormatter(Formatter(
        '%(asctime)s %(levelname)s: %(message)s '
        '[in %(pathname)s:%(lineno)d]')
    )
    app.logger.addHandler(mail_handler)
示例#5
0
def init(app):

    if app.config.get('ERROR_EMAIL'):

        if app.debug:
            logging.getLogger().setLevel(logging.DEBUG)
        else:
            log_format = logging.Formatter('''
    ---
    Message type:       %(levelname)s
    Location:           %(pathname)s:%(lineno)d
    Module:             %(module)s
    Function:           %(funcName)s
    Time:               %(asctime)s

%(message)s

    ''')

            # Send errors via email
            mail_handler = SMTPHandler('127.0.0.1',
                                       app.config.get('DEFAULT_MAIL_SENDER'),
                                       app.config.get('ERROR_EMAIL'), 'Error')
            mail_handler.setLevel(logging.ERROR)
            mail_handler.setFormatter(log_format)

            # Also continue to log errors to stderr
            stream_handler = logging.StreamHandler()
            stream_handler.setFormatter(log_format)

            app.logger.addHandler(stream_handler)
            app.logger.addHandler(mail_handler)
示例#6
0
def configure_logging(app):
    '''Setup file(info) and email(error) logging'''
    # Return if in debug or testing mode
    if app.debug or app.testing:
        return

    import logging
    from logging.handlers import RotatingFileHandler, SMTPHandler

    # Set logging level to info
    app.logger.setLevel(logging.INFO)

    # Rotating File loggiing for (info) level
    debug_log = os.path.join(app.root_path, app.config['DEBUG_LOG'])
    file_handler = RotatingFileHandler(debug_log, maxBytes=100000, backupCount=10)
    file_handler.setLevel(logging.DEBUG)
    file_handler.setFormatter(logging.Formatter(
        '%(asctime)s %(processName)s\t | %(levelname)s: %(message)s '
        '[in %(pathname)s:%(lineno)d]')
    )
    app.logger.addHandler(file_handler)

    # Mail logging haldler for (error) level
    mail_handler = SMTPHandler(app.config['MAIL_SERVER'],
                               app.config['MAIL_USERNAME'],
                               app.config['SITE_ADMINS'],
                               'O_ops... %s failed!' % app.config['SITE_NAME'],
                               (app.config['MAIL_USERNAME'],
                                app.config['MAIL_PASSWORD']))
    mail_handler.setLevel(logging.ERROR)
    mail_handler.setFormatter(logging.Formatter(
        '%(asctime)s %(processName)s\t | %(levelname)s: %(message)s '
        '[in %(pathname)s:%(lineno)d]')
    )
    app.logger.addHandler(mail_handler)
示例#7
0
文件: init_app.py 项目: rcharp/Simple
def init_error_logger_with_email_handler(app):
    """
    Initialize a logger to send emails on error-level messages.
    Unhandled exceptions will now send an email message to app.config.ADMINS.
    """
    if app.debug: return                        # Do not send error emails while developing

    # Retrieve email settings from app.config
    host      = app.config['MAIL_SERVER']
    port      = app.config['MAIL_PORT']
    from_addr = app.config['MAIL_DEFAULT_SENDER']
    username  = app.config['MAIL_USERNAME']
    password  = app.config['MAIL_PASSWORD']
    secure = () if app.config.get('MAIL_USE_TLS') else None

    # Retrieve app settings from app.config
    to_addr_list = app.config['ADMINS']
    subject = app.config.get('APP_SYSTEM_ERROR_SUBJECT_LINE', 'System Error')

    # Setup an SMTP mail handler for error-level messages
    mail_handler = SMTPHandler(
        mailhost=(host, port),                  # Mail host and port
        fromaddr=from_addr,                     # From address
        toaddrs=to_addr_list,                   # To address
        subject=subject,                        # Subject line
        credentials=(username, password),       # Credentials
        secure=secure,
    )
    mail_handler.setLevel(logging.ERROR)
    app.logger.addHandler(mail_handler)
示例#8
0
def create_app(name, config_filename):
    app = Flask(name,
                template_folder='app/templates',
                static_folder='app/static')
    app.config.from_pyfile(config_filename)

    from app.models import db
    db.init_app(app)
    db.app = app
    from app.sms import mail
    mail.init_app(app)
    mail.app = app

    from app.views import main
    app.register_blueprint(main)

    #TODO: Secure admin view
    admin = Admin()
    admin.init_app(app)
    admin.app = app
    admin.add_view(ModelView(User, db.session))

    if not (app.debug or app.config.get('LOGGING_DISABLE', False)):
        import logging
        from logging import Formatter
        from logging.handlers import SMTPHandler, RotatingFileHandler
        mail_handler = SMTPHandler(mailhost=app.config['MAIL_SERVER'],
                                   fromaddr=app.config['LOGGING_SENDER'],
                                   toaddrs=app.config['ADMINS'],
                                   subject='dci-notify Server Error',
                                   credentials=(app.config['MAIL_USERNAME'],
                                                app.config['MAIL_PASSWORD']))

        file_handler = RotatingFileHandler(filename=os.path.join(basedir,
                                                                 'app.log'),
                                           maxBytes=1048756,
                                           backupCount=5)

        mail_handler.setLevel(logging.ERROR)
        file_handler.setLevel(logging.WARNING)
        mail_handler.setFormatter(Formatter('''
            Message type:       %(levelname)s
            Location:           %(pathname)s:%(lineno)d
            Module:             %(module)s
            Function:           %(funcName)s
            Time:               %(asctime)s

            Message:

            %(message)s
        '''))

        file_handler.setFormatter(Formatter(
            '%(asctime)s %(levelname)s: %(message)s '
            '[in %(pathname)s:%(lineno)d]'
        ))
        app.logger.addHandler(mail_handler)
        app.logger.addHandler(file_handler)

    return app
示例#9
0
文件: app.py 项目: bbonamin/Skylines
    def add_logging_handlers(self):
        if self.debug: return

        import logging
        from logging import Formatter, Filter
        from logging.handlers import RotatingFileHandler, SMTPHandler

        # Set general log level
        self.logger.setLevel(logging.INFO)

        # Inject additional log record fields
        class ContextFilter(Filter):
            def __init__(self, app):
                self.app = app

            def filter(self, record):
                record.app_name = self.app.name
                record.url = request.url
                record.ip = request.remote_addr
                if g.current_user:
                    user = g.current_user
                    record.user = '******' % (user.name, user.email_address)
                else:
                    record.user = '******'

                return True

        self.logger.addFilter(ContextFilter(self))

        # Add SMTP handler
        mail_handler = SMTPHandler(
            'localhost', '*****@*****.**',
            self.config.get('ADMINS', []), 'SkyLines Error Report')
        mail_handler.setLevel(logging.ERROR)

        mail_formatter = Formatter('''
App:                %(app_name)s
Time:               %(asctime)s
URL:                %(url)s
IP:                 %(ip)s
User:               %(user)s

Message:

%(message)s
''')
        mail_handler.setFormatter(mail_formatter)
        self.logger.addHandler(mail_handler)

        # Add log file handler (if configured)
        path = self.config.get('LOGFILE')
        if path:
            file_handler = RotatingFileHandler(path, 'a', 10000, 4)
            file_handler.setLevel(logging.INFO)

            file_formatter = Formatter(
                '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')
            file_handler.setFormatter(file_formatter)

            self.logger.addHandler(file_handler)
示例#10
0
    def init_app(cls, app):
        Config.init_app(app)

        import logging
        from logging.handlers import SMTPHandler
        from logging import StreamHandler

        # email errors to admin
        credentials = None
        secure = None
        if getattr(cls, "MAIL_USERNAME", None) is not None:
            credentials = (cls.MAIL_USERNAME, cls.MAIL_PASSWORD)
            if getattr(cls, "MAIL_USE_TLS", None):
                secure = ()
        mail_handler = SMTPHandler(
            mailhost=(cls.MAIL_SERVER, cls.MAIL_PORT),
            fromaddr=cls.MAIL_SENDER,
            toaddrs=[cls.ADMIN],
            subject=cls.MAIL_SUBJECT_PREFIX + u"Application Error",
            credentials=credentials,
            secure=secure,
        )
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)

        # log to stderr
        file_handler = StreamHandler()
        file_handler.setLevel(logging.WARNING)
        app.logger.addHandler(file_handler)

        # handle proxy server headers
        from werkzeug.contrib.fixers import ProxyFix

        app.wsgi_app = ProxyFix(app.wsgi_app)
    def init_app(self, app):
        handlers = []
        filehandler = RotatingFileHandler(filename=app.config['LOG_FILE_LOC'],
                                          maxBytes=1000000, backupCount=5)
        formatter = logging.Formatter(
            "[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s")
        filehandler.setLevel(logging.INFO)
        if app.config['TESTING']:
            filehandler.setLevel(logging.ERROR)
        filehandler.setFormatter(formatter)

        logging.basicConfig()
        handlers.append(filehandler)

        if not app.debug:
            mail_handler = SMTPHandler(
                (app.config['INTERNAL_MAILS_SERVER'],
                 app.config['INTERNAL_MAILS_PORT']),
                app.config['INTERNAL_MAILS_SERVER_USERNAME'],
                app.config['NIGHTS_WATCH'],
                app.config.get('SERVER_ERROR_MAIL_SUBJECT',
                               'Server error'),
                credentials=(app.config['INTERNAL_MAILS_SERVER_USERNAME'],
                             app.config['INTERNAL_MAILS_SERVER_PASSWORD']),
                secure=())
            mail_handler.setLevel(logging.ERROR)
            mail_handler.setFormatter(formatter)
            handlers.append(mail_handler)
        self.logging_queue_listener = QueueListener(
            self.logging_queue, *handlers)
        app.logger.addHandler(self.logging_queue_handler)
示例#12
0
文件: log.py 项目: FineUploader/docfu
def init(level=None, logger=getLogger(), handler=StreamHandler(), development=True):
    logging.basicConfig(level=level, datefmt='%m-%d %H:%M')
    logger = logging.getLogger()

    if (os.isatty(sys.stdout.fileno())
            and not sys.platform.startswith('win')):
        fmt = ANSIFormatter()
    else:
        fmt = TextFormatter()
    handler.setFormatter(fmt)
    logger.addHandler(handler)

    if level:
        logger.setLevel(level)

    if not development:
        logger.info("Setting up email handler for production-mode.")
        fmt = EmailFormatter()
        smtpHandler = SMTPHandler('127.0.0.1', '*****@*****.**',
            ['*****@*****.**'], 'ALERT docfu error!')
        smtpHandler.setFormatter(fmt)
        smtpHandler.setLevel(logging.ERROR)
        logger.addHandler(smtpHandler)

    return logger
示例#13
0
文件: config.py 项目: Jeffiy/zblog
    def init_app(cls, app):
        Config.init_app(app)

        # send error log to admin'email
        import logging
        from logging.handlers import SMTPHandler

        # log to stderr
        from logging import StreamHandler
        file_handler = StreamHandler()
        file_handler.setLevel(logging.WARNING)
        app.logger.addHandler(file_handler)

        secure = None
        if getattr(cls, 'MAIL_USERNAME', None) is not None:
            credentials = (cls.MAIL_USERNAME, cls.MAIL_PASSWORD)
            if getattr(cls, 'MAIL_USER_TLS', None):
                secure = ()
            mail_hanlder = SMTPHandler(
                mailhost=(cls.MAIL_SERVER, cls.MAIL_PORT),
                fromaddr=cls.ZBLOG_MAIL_SENDER,
                toaddrs=[cls.ZBLOG_ADMIN],
                subject=cls.ZBLOG_MAIL_SUBJECT_PREFIX + 'Application Error',
                credentials=credentials,
                secure=secure
            )
            mail_hanlder.setLevel(logging.ERROR)
            app.logger.addHandler(mail_hanlder)
def exception_handler(app):
    """
    Register 0 or more exception handlers (mutates the app passed in).

    :param app: Flask application instance
    :return: None
    """
    # This will not execute when debug is set to True.
    mail_handler = SMTPHandler((app.config.get('MAIL_SERVER'),
                                app.config.get('MAIL_PORT')),
                               '*****@*****.**',
                               [app.config.get('MAIL_USERNAME')],
                               '[Exception handler] A 5xx was thrown',
                               (app.config.get('MAIL_USERNAME'),
                                app.config.get('MAIL_PASSWORD')),
                               secure=())

    mail_handler.setLevel(logging.ERROR)
    mail_handler.setFormatter(logging.Formatter('''
    Time:         %(asctime)s
    Message type: %(levelname)s


    Message:

    %(message)s
    '''))
    app.logger.addHandler(mail_handler)

    return None
示例#15
0
文件: config.py 项目: jyundt/oval
    def init_app(cls, app):
        Config.init_app(app)
        import logging
        from app.log import ContextFilter
        from logging import Formatter
        from logging.handlers import SMTPHandler, RotatingFileHandler
        credentials = None
        secure = None
        if getattr(cls, 'MAIL_USERNAME', None) is not None:
            credentials = (cls.MAIL_USERNAME, cls.MAIL_PASSWORD)
            if getattr(cls, 'MAIL_USE_TLS', None):
                secure = ()

        mail_handler = SMTPHandler(
            mailhost=(cls.MAIL_SERVER, cls.MAIL_PORT),
            fromaddr=cls.MAIL_SENDER,
            toaddrs=[cls.MAIL_FEEDBACK_ADDRESS],
            subject=cls.MAIL_SUBJECT_PREFIX + ' Application Error',
            credentials=credentials,
            secure=secure)
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)

        context_provider = ContextFilter()
        app.logger.addFilter(context_provider)
        audit_log_handler = RotatingFileHandler(cls.AUDIT_LOG, maxBytes=131072,
                                                backupCount=5)
        audit_log_handler.setLevel(logging.INFO)
        audit_log_handler.setFormatter(Formatter('%(asctime)s [%(levelname)s] %(ip)s %(admin_username)s[%(admin_id)d]: %(blueprint)s-%(funcName)s %(message)s'))
        app.logger.addHandler(audit_log_handler)

        app.logger.setLevel(logging.INFO) 
示例#16
0
文件: core.py 项目: occrp/spindle
def create_app(config={}):
    app = Flask("spindle")
    app.config.from_object(default_settings)
    app.config.from_envvar("SPINDLE_SETTINGS", silent=True)
    app.config.update(config)

    config = {
        "schemas": app.config.get("SCHEMAS"),
        "database": app.config.get("DATABASE_URI"),
        "elastic_host": app.config.get("ELASTICSEARCH_HOST"),
        "elastic_index": app.config.get("ELASTICSEARCH_INDEX"),
    }
    app.loom_config = Config(config)
    load_local_schema(app.loom_config.resolver)
    app.loom_config.setup()

    assets.init_app(app)
    oauth.init_app(app)

    if not app.debug and app.config.get("MAIL_ADMINS"):
        app_name = app.config.get("APP_NAME")
        credentials = app.config.get("MAIL_CREDENTIALS", ())
        mail_handler = SMTPHandler(
            app.config.get("MAIL_HOST"),
            app.config.get("MAIL_FROM"),
            app.config.get("MAIL_ADMINS"),
            "%s crash report" % app_name,
            credentials=credentials,
            secure=(),
        )
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)
    return app
示例#17
0
def config_logging(app):
	info_log = os.path.join(app.root_path, app.config['INFO_LOG'])
	error_log = os.path.join(app.root_path, app.config['ERROR_LOG'])

	formatter = logging.Formatter(
	'''%(asctime)s %(levelname)s [in %(pathname)s:%(lineno)d]:
	%(message)s ''')

	if app.config['TESTING'] == False:

		error_handler = RotatingFileHandler(error_log, maxBytes=102400,
			backupCount=10)
		error_handler.setLevel(logging.ERROR)
		error_handler.setFormatter(formatter)
		app.logger.addHandler(error_handler)

		if app.debug == True:
			info_handler = RotatingFileHandler(info_log, maxBytes=102400, backupCount=10)
			info_handler.setLevel(logging.INFO)
			info_handler.setFormatter(formatter)
			app.logger.addHandler(info_handler)
		else:
			from logging.handlers import SMTPHandler
			mail_handler = SMTPHandler(mailhost=app.config['MAILHOST'],
				fromaddr=app.config['FROMADDR'], toaddrs=app.config['TOADDR'],
				subject='Server Error', credentials=(app.config['FROMADDR'],
					app.config['PASSWORD']))
			mail_handler.setLevel(logging.ERROR)
			app.logger.addHandler(mail_handler)
示例#18
0
def ErrorMail():
    if not app.debug:
        import logging
        from logging.handlers import SMTPHandler
        credentials=None
        if MAIL_USERNAME or MAIL_PASSWORD:
            credentials=(MAIL_USERNAME,MAIL_PASSWORD)
        mail_handler = SMTPHandler(MAIL_PORT,
                                   'no-reply@' + MAIL_SERVER,
                                   ADMINS, 'microblg failture',credentials)
        from logging import Formatter
        mail_handler.setFormatter(Formatter('''

Messagetype:        %(levelname)s
Location:           %(pathname)s:%(lineno)d
Module:             %(module)s
Function:           %(funcName)s
Time:               %(asctime)s

Message:

%(message)s
'''))
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)
示例#19
0
def init(app):
    """
    Initialize logging for the application, (only if its not in debug mode)
    """
    if not app.debug:
        from logging import Formatter
        from logging.handlers import SMTPHandler, TimedRotatingFileHandler
        
        # File handler
        file_formatter = Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')
        file_handler = TimedRotatingFileHandler(app.config['LOG_FILE_NAME'], when='midnight', backupCount=31)
        file_handler.setFormatter(file_formatter)
        file_handler.setLevel(app.config['LOG_FILE_LEVEL'])
        app.logger.addHandler(file_handler)
        
        # Email handler
        mail_formatter = Formatter('''
Message type:       %(levelname)s
Location:           %(pathname)s:%(lineno)d
Module:             %(module)s
Function:           %(funcName)s
Time:               %(asctime)s

Message:

%(message)s
''')
        mail_handler = SMTPHandler(app.config['LOG_EMAIL_SERVER'], app.config['LOG_EMAIL_SENDER'],
                                   app.config['ADMIN_EMAILS'].split(','), '[%s] Error' % app.config['HOST_DOMAIN'])
        mail_handler.setFormatter(mail_formatter)
        mail_handler.setLevel(app.config['LOG_EMAIL_LEVEL'])
        app.logger.addHandler(mail_handler)
        
示例#20
0
文件: app.py 项目: kevana/corpscores
def register_loggers(app):
    if not (app.debug or app.config.get('LOGGING_DISABLE', False)):
        import logging
        from logging import Formatter
        from logging.handlers import SMTPHandler
        mail_handler = SMTPHandler(mailhost=app.config['MAIL_SERVER'],
                                   fromaddr=app.config['LOGGING_SENDER'],
                                   toaddrs=app.config['ADMINS'],
                                   subject='CorpScores Server Error',
                                   credentials=(app.config['MAIL_USERNAME'],
                                                app.config['MAIL_PASSWORD']))
        mail_handler.setLevel(logging.ERROR)
        mail_handler.setFormatter(Formatter('''
            Message type:       %(levelname)s
            Location:           %(pathname)s:%(lineno)d
            Module:             %(module)s
            Function:           %(funcName)s
            Time:               %(asctime)s

            Message:

            %(message)s
        '''))
        app.logger.addHandler(mail_handler)
    return None
示例#21
0
def configure_logging(app):
    """Configure file(info) and email(error) logging."""

    if app.debug or app.testing:
        # skip debug and test mode.
        return

    import logging
    from logging.handlers import SMTPHandler

    # Set info level on logger, which might be overwritten by handers.
    app.logger.setLevel(logging.INFO)

    debug_log = os.path.join(app.root_path, app.config['DEBUG_LOG'])
    file_handler = logging.handlers.RotatingFileHandler(debug_log, maxBytes=100000, backupCount=10)
    file_handler.setLevel(logging.DEBUG)
    file_handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s '
        '[in %(pathname)s:%(lineno)d]')
    )
    app.logger.addHandler(file_handler)

    ADMINS = ['*****@*****.**']
    mail_handler = SMTPHandler(app.config['MAIL_SERVER'],
                               app.config['MAIL_USERNAME'],
                               ADMINS,
                               'Error: Satellite',
                               (app.config['MAIL_USERNAME'],
                                app.config['MAIL_PASSWORD']))
    mail_handler.setLevel(logging.ERROR)
    mail_handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s '
        '[in %(pathname)s:%(lineno)d]')
    )
    app.logger.addHandler(mail_handler)
示例#22
0
def configure_logging(app):
    if app.debug or app.testing:
        return

    mail_handler = SMTPHandler(
        app.config["MAIL_SERVER"],
        "*****@*****.**",
        app.config["ADMINS"],
        "application error",
        (app.config["MAIL_USERNAME"], app.config["MAIL_PASSWORD"]),
    )

    mail_handler.setLevel(logging.ERROR)
    app.logger.addHandler(mail_handler)

    formatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s " "[in %(pathname)s:%(lineno)d]")

    debug_log = os.path.join(app.root_path, app.config["DEBUG_LOG"])

    debug_file_handler = RotatingFileHandler(debug_log, maxBytes=100000, backupCount=10)

    debug_file_handler.setLevel(logging.DEBUG)
    debug_file_handler.setFormatter(formatter)
    app.logger.addHandler(debug_file_handler)

    error_log = os.path.join(app.root_path, app.config["ERROR_LOG"])

    error_file_handler = RotatingFileHandler(error_log, maxBytes=100000, backupCount=10)

    error_file_handler.setLevel(logging.ERROR)
    error_file_handler.setFormatter(formatter)
    app.logger.addHandler(error_file_handler)
示例#23
0
def register_error_logger(app):
    if not app.debug:
        import logging
        from logging.handlers import SMTPHandler
        mail_handler = SMTPHandler(
            ('smtp.gmail.com','587'),
            '[Sistema Impresion] ' + date.today().strftime("%d-%m-%Y"),
            ADMINS,
            'Error!',
            ('*****@*****.**', '*****'),
            tuple()
        )
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)    
    
        mail_handler.setFormatter(Formatter('''
                Message type:       %(levelname)s
                Location:           %(pathname)s:%(lineno)d
                Module:             %(module)s
                Function:           %(funcName)s
                Time:               %(asctime)s
                
                Message:
                
                %(message)s
            ''')
        )
示例#24
0
def configure_logging(app):
    """Configure file(info) and email(error) logging."""

    if app.debug or app.testing:
        # skip debug and test mode.
        return

    import logging
    from logging.handlers import RotatingFileHandler, SMTPHandler

    # Set info level on logger, which might be overwritten by handers.
    app.logger.setLevel(logging.INFO)

    debug_log = os.path.join(app.root_path, app.config["DEBUG_LOG"])
    file_handler = logging.handlers.RotatingFileHandler(debug_log, maxBytes=100000, backupCount=10)
    file_handler.setLevel(logging.DEBUG)
    file_handler.setFormatter(
        logging.Formatter("%(asctime)s %(levelname)s: %(message)s " "[in %(pathname)s:%(lineno)d]")
    )
    app.logger.addHandler(file_handler)

    ADMINS = ["*****@*****.**"]
    mail_handler = SMTPHandler(
        app.config["MAIL_SERVER"],
        app.config["MAIL_USERNAME"],
        ADMINS,
        "O_ops... %s failed!" % PROJECT,
        (app.config["MAIL_USERNAME"], app.config["MAIL_PASSWORD"]),
    )
    mail_handler.setLevel(logging.ERROR)
    mail_handler.setFormatter(
        logging.Formatter("%(asctime)s %(levelname)s: %(message)s " "[in %(pathname)s:%(lineno)d]")
    )
    app.logger.addHandler(mail_handler)
示例#25
0
def configure_app():
    '''Configure app loading in order from:

    [annotator.settings_default]
    [annotator.settings_local]
    annotator.cfg # in app root dir
    config file specified by env var ANNOTATOR_CONFIG
    '''
    # app.config.from_object('annotator.settings_default')
    # app.config.from_object('annotator.settings_local')
    here = os.path.dirname(os.path.abspath( __file__ ))
    # parent directory
    config_path = os.path.join(os.path.dirname(here), 'annotator.cfg')
    if os.path.exists(config_path):
        app.config.from_pyfile(config_path)
    if 'ANNOTATOR_CONFIG' in os.environ:
        app.config.from_envvar('ANNOTATOR_CONFIG')
    ADMINS = app.config.get('ADMINS', '')
    if not app.debug and ADMINS:
        import logging
        from logging.handlers import SMTPHandler
        mail_handler = SMTPHandler('127.0.0.1',
                                   '*****@*****.**',
                                   ADMINS, 'annotator error')
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)
示例#26
0
class SchedulerApp(object):

    def __init__(self, args):
        super(SchedulerApp, self).__init__()
        self.args = args
        config = Config.getInstance()
        worker = config.getWorkerName();
        self.mailer = SMTPHandler(config.getSmtpServer(),
                                  'scheduler@%s' % worker,
                                  config.getSupportEmail(),
                                  "[indico_scheduler] Problem at %s" % worker)

        self.mailer.setLevel(logging.ERROR)

    def run(self):
        root_logger = logging.getLogger('')
        root_logger.addHandler(self.mailer)

        logger = logging.getLogger('daemon')
        try:
            Scheduler(multitask_mode = self.args.mode).run()
            return_val = 0
        except base.SchedulerQuitException:
            logger.info("Daemon shut down successfully")
            return_val = 0
        except:
            logger.exception("Daemon terminated for unknown reason ")
            return_val = -1
        finally:
            return return_val
示例#27
0
def configure_app(config_file):
    app.config.from_pyfile(config_file)

    mysql_db.set_config(    app.config['DB_HOST'],
                            app.config['DB_USERNAME'],
                            app.config['DB_PASSWORD'],
                            app.config['DB_DATABASE']  )

    commonsense.set_config( app.config['CS_SERVER'],
                            app.config['CS_VERBOSITY'],
                          )

    # Setup logging
    if not app.config['DEBUG']:
        import logging
        from logging.handlers import SMTPHandler

        # log everything >= DEBUG to a file
        file_handler = logging.FileHandler("/var/log/AuthAPI/tma-api.log")
        file_handler.setLevel(logging.DEBUG)
        app.logger.addHandler(file_handler)

        # log errors by email
        email_handler = SMTPHandler(    '127.0.0.1',
                                        "ADMIN_EMAIL",
                                        app.config['ADMINS'],
                                        "Exception in AuthAPI")
        email_handler.setLevel(logging.ERROR)
        app.logger.addHandler(email_handler)
def init_logging(app):
    """Initialize app error logging

    :type app: flask.Flask
    :param app: The application to configure.
    """
    if app.debug:
        return

    ADMINS = ['*****@*****.**']
    import logging
    from logging.handlers import SMTPHandler

    credentials = None
    secure = None
    if app.config.get("MAIL_USERNAME") is not None:
        credentials = (app.config["MAIL_USERNAME"], app.config.get("MAIL_PASSWORD"))
        if app.config.get("MAIL_USE_TLS") is not None or app.config.get("MAIL_USE_SSL") is not None:
            secure = tuple()

    mail_handler = SMTPHandler(app.config["MAIL_SERVER"],
                               app.config["ERROR_SENDER"],
                               app.config["ERROR_ADDRESS"],
                               app.config["ERROR_SUBJECT"],
                               credentials=credentials, secure=secure)

    mail_handler.setLevel(logging.ERROR)
    if app.config.get("ERROR_FORMAT") is not None:
        mail_handler.setFormatter(Formatter(app.config["ERROR_FORMAT"]))

    for log in (getLogger('sqlalchemy'), app.logger):
        log.addHandler(mail_handler)
示例#29
0
    def init_app(cls, app):
        Config.init_app(app)

        # handle proxy server headers
        from werkzeug.contrib.fixers import ProxyFix
        app.wsgi_app = ProxyFix(app.wsgi_app)

        # email errors to the administrators
        import logging
        from logging.handlers import SMTPHandler
        credentials = None
        secure = None
        if getattr(cls, 'MAIL_USERNAME', None) is not None:
            credentials = (cls.MAIL_USERNAME, cls.MAIL_PASSWORD)
            if getattr(cls, 'MAIL_USE_TLS', None):
                secure = ()
        mail_handler = SMTPHandler(
            mailhost=(cls.MAIL_SERVER, cls.MAIL_PORT),
            fromaddr=cls.PORTAL_MAIL_SENDER,
            toaddrs=[cls.PORTAL_ADMIN],
            subject=cls.PORTAL_MAIL_SUBJECT_PREFIX + ' Application Error',
            credentials=credentials,
            secure=secure)
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)
def create_app(config_name):
    """Create an application instance."""
    app = Flask(__name__)

    # import configuration
    cfg = os.path.join(os.getcwd(), 'config', config_name + '.py')
    app.config.from_pyfile(cfg)

    # initialize extensions
    bootstrap.init_app(app)
    db.init_app(app)
    lm.init_app(app)

    # import blueprints
    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    # configure production logging of errors
    if not app.config['DEBUG'] and not app.config['TESTING']:
        import logging
        from logging.handlers import SMTPHandler
        mail_handler = SMTPHandler('127.0.0.1', '*****@*****.**',
                                   app.config['ADMINS'], 'Application Error')
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)

    return app
def configureRootLogger(args):
	"""
	Configure the main logger
	Parameters:
	- args: (dictionary) args from the command line

	Returns the logger
	"""
	# store the configuration in Config (datat used elsewhere)
	gameName = args['<gameName>']
	Config.mode = 'prod' if args['--prod'] else 'dev' if args['--dev'] else 'debug'
	Config.logPath = join('games', gameName, Template(args['--log']).render(hostname=gethostname()))
	Config.webPort = args['--web']
	Config.host = args['--host']

	# add the LOW_DEBUG and MESSAGE logging levels
	logging.addLevelName(LOW_DEBUG_LEVEL, "COM_DEBUG")
	logging.Logger.low_debug = low_debug
	logging.addLevelName(MESSAGE_LEVEL, "MESSAGE")
	logging.Logger.message = message

	# Create and setup the logger
	logger = logging.getLogger()
	logger.setLevel(LOW_DEBUG_LEVEL)

	# add an handler to redirect the log to a file (1Mo max)
	makedirs(Config.logPath, exist_ok=True)
	file_handler = RotatingFileHandler(join(Config.logPath, 'activity.log'), mode='a',
	                                   maxBytes=MAX_ACTIVITY_SIZE, backupCount=1)
	file_handler.setLevel(activity_level[Config.mode][1])
	file_formatter = logging.Formatter('%(asctime)s [%(name)s] | %(message)s', "%m/%d %H:%M:%S")
	file_handler.setFormatter(file_formatter)
	logger.addHandler(file_handler)

	# Add an other handler to redirect some logs to the console
	# (with colors, depending on the level DEBUG/INFO/WARNING/ERROR/CRITICAL)
	steam_handler = logging.StreamHandler()
	steam_handler.setLevel(activity_level[Config.mode][0])
	LOGFORMAT = "  %(log_color)s[%(name)s]%(reset)s | %(log_color)s%(message)s%(reset)s"
	formatter = ColoredFormatter(LOGFORMAT)
	steam_handler.setFormatter(formatter)
	logger.addHandler(steam_handler)

	# An other handler to log the errors (only) in errors.log
	error_handler = RotatingFileHandler(join(Config.logPath, 'errors.log'), mode='a',
	                                    maxBytes=MAX_ACTIVITY_SIZE, backupCount=1)
	error_handler.setLevel(error_level[Config.mode])
	error_formatter = logging.Formatter('----------------------\n%(asctime)s [%(name)s] | %(message)s', "%m/%d %H:%M:%S")
	error_handler.setFormatter(error_formatter)
	logger.addHandler(error_handler)

	# Manage errors (send an email) when we are in production
	if Config.mode == 'prod' and not args['--no-email']:
		# get the password (and disable warning message)
		# see http://stackoverflow.com/questions/35408728/catch-warning-in-python-2-7-without-stopping-part-of-progam
		# noinspection PyUnusedLocal
		def custom_fallback(prompt="Password: "******"""Simple fallback to get the password, see getpass module"""
			print("WARNING: Password input may be echoed (can not control echo on the terminal)")
			# noinspection PyProtectedMember
			return getpass._raw_input(prompt)  # Use getpass' custom raw_input function for security

		getpass.fallback_getpass = custom_fallback  # Replace the getpass.fallback_getpass function with our equivalent
		password = getpass.getpass('Password for %s account:' % args['--email'])
		# check the smtp and address
		smtp, port = 0, ''
		try:
			smtp, port = args['--smtp'].split(':')
			port = int(port)
		except ValueError:
			print(Fore.RED + "Error: The smtp is not valid (should be `smpt:port`)" + Fore.RESET)
			quit()
		address = parseaddr(args['--email'])[1]
		if not address:
			print(Fore.RED + "Error: The email address is not valid" + Fore.RESET)
			quit()
		# check if the password/email/smtp works
		smtpserver = SMTP(smtp, port)
		smtpserver.ehlo()
		smtpserver.starttls()
		try:
			smtpserver.login(address, password)
		except SMTPAuthenticationError as err:
			print(Fore.RED + "Error: The email/smtp:port/password is not valid address is not valid (%s)" % err + Fore.RESET)
			quit()
		finally:
			smtpserver.close()

		# add an other handler to redirect errors through emails
		mail_handler = SMTPHandler((smtp, port), address, [address], "Error in CGS (%s)" % gethostname(),
		                           (address, password), secure=())
		mail_handler.setLevel(activity_level[Config.mode][2])
		# mail_formatter = logging.Formatter('%(asctime)s [%(name)s] | %(message)s', "%m/%d %H:%M:%S")
		# mail_handler.setFormatter(mail_formatter)
		logger.addHandler(mail_handler)

	return logger
示例#32
0
def create_app(config={}):
    app = Flask('aleph')
    app.config.from_object(default_settings)
    app.config.from_envvar('ALEPH_SETTINGS', silent=True)
    app.config.update(config)
    app_name = app.config.get('APP_NAME')

    if app.config.get("TESTING"):
        # The testing configuration is inferred from the production
        # settings, but it can only be derived after the config files
        # have actually been evaluated.
        database_uri = app.config.get('SQLALCHEMY_DATABASE_URI')
        app.config['SQLALCHEMY_DATABASE_URI'] = database_uri + '_test'

        es_index = app.config.get('ELASTICSEARCH_INDEX',
                                  app.config.get('APP_NAME'))
        app.config['ELASTICSEARCH_INDEX'] = es_index + '_test'

    if not app.debug and app.config.get('MAIL_ADMINS'):
        credentials = (app.config.get('MAIL_USERNAME'),
                       app.config.get('MAIL_PASSWORD'))
        mail_handler = SMTPHandler(app.config.get('MAIL_SERVER'),
                                   app.config.get('MAIL_FROM'),
                                   app.config.get('MAIL_ADMINS'),
                                   '[%s] Crash report' % app_name,
                                   credentials=credentials,
                                   secure=())
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)

    if 'postgres' not in app.config.get('SQLALCHEMY_DATABASE_URI', ''):
        raise RuntimeError("aleph database must be PostgreSQL!")

    queues = (
        Queue(WORKER_QUEUE, routing_key=WORKER_ROUTING_KEY),
        Queue(USER_QUEUE, routing_key=USER_ROUTING_KEY),
    )
    celery.conf.update(
        imports=('aleph.queues'),
        broker_url=app.config['CELERY_BROKER_URL'],
        task_always_eager=app.config['CELERY_ALWAYS_EAGER'],
        task_eager_propagates=True,
        task_ignore_result=True,
        result_persistent=False,
        task_queues=queues,
        task_default_queue=WORKER_QUEUE,
        task_default_routing_key=WORKER_ROUTING_KEY,
        # ultra-high time limit to shoot hung tasks:
        task_time_limit=3600 * 3,
        worker_max_tasks_per_child=200,
        worker_disable_rate_limits=True,
        beat_schedule=app.config['CELERYBEAT_SCHEDULE'],
    )
    celery.conf.update(app.config.get('CELERY', {}))

    migrate.init_app(app, db, directory=app.config.get('ALEMBIC_DIR'))
    configure_oauth(app)
    mail.init_app(app)
    db.init_app(app)

    try:
        ldap.init_app(app)
    except LDAPException as error:
        log.info(error)

    # This executes all registered init-time plugins so that other
    # applications can register their behaviour.
    for plugin in get_init():
        plugin(app=app)
    return app
示例#33
0
def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    db.init_app(app)
    migrate.init_app(app, db)
    login.init_app(app)
    mail.init_app(app)
    bootstrap.init_app(app)
    moment.init_app(app)
    babel.init_app(app)
    app.elasticsearch = Elasticsearch([app.config['ELASTICSEARCH_URL']]) \
        if app.config['ELASTICSEARCH_URL'] else None
    app.redis = Redis.from_url(app.config['REDIS_URL'])
    app.task_queue = rq.Queue('microblog-tasks', connection=app.redis)

    from app.errors import bp as errors_bp
    app.register_blueprint(errors_bp)

    from app.auth import bp as auth_bp
    app.register_blueprint(auth_bp, url_prefix='/auth')

    from app.main import bp as main_bp
    app.register_blueprint(main_bp)

    from app.api import bp as api_bp
    app.register_blueprint(api_bp, url_prefix='/api')

    if not app.debug and not app.testing:
        if app.config['MAIL_SERVER']:
            auth = None
            if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
                auth = (app.config['MAIL_USERNAME'],
                        app.config['MAIL_PASSWORD'])
            secure = None
            if app.config['MAIL_USE_TLS']:
                secure = ()
            mail_handler = SMTPHandler(
                mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
                fromaddr='no-reply@' + app.config['MAIL_SERVER'],
                toaddrs=app.config['ADMINS'], subject='Microblog Failure',
                credentials=auth, secure=secure)
            mail_handler.setLevel(logging.ERROR)
            app.logger.addHandler(mail_handler)

        if app.config['LOG_TO_STDOUT']:
            stream_handler = logging.StreamHandler()
            stream_handler.setLevel(logging.INFO)
            app.logger.addHandler(stream_handler)
        else:
            if not os.path.exists('logs'):
                os.mkdir('logs')
            file_handler = RotatingFileHandler('logs/microblog.log',
                                               maxBytes=10240, backupCount=10)
            file_handler.setFormatter(logging.Formatter(
                '%(asctime)s %(levelname)s: %(message)s '
                '[in %(pathname)s:%(lineno)d]'))
            file_handler.setLevel(logging.INFO)
            app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)
        app.logger.info('Microblog startup')

    return app
示例#34
0
import pdb

from logging.handlers import SMTPHandler
from creds import USER, PASS

# logger
logger = logging.getLogger('Alert - email')

# Handler
ch = SMTPHandler(('smtp.gmail.com', 465),
                 '*****@*****.**',
                 '*****@*****.**',
                 'Alert - Email ',
                 credentials=(USER, PASS),
                 secure='none')
ch.setLevel(logging.DEBUG)

# Logger and handler
logger.addHandler(ch)

if __name__ == '__main__':
    #disk_size = int(raw_input("please enter the disk size to check ?"))
    p1 = Popen(["df", "-h", "/"], stdout=PIPE)
    p2 = Popen(["tail", "-n", "1"], stdin=p1.stdout, stdout=PIPE)
    output = p2.communicate()[0]
    disk_size = int(re.search('([0-9]+)%', output).group(1))

    pdb.set_trace()
    if disk_size < 50:
        logger.info("The disk looks health at {}".format(disk_size))
    elif disk_size < 70:
示例#35
0
if not app.debug:
    if app.config['MAIL_SERVER']:
        auth = None
        if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
            auth = (app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])
        secure = None
        if app.config['MAIL_USE_TLS']:
            secure = ()
        mail_handler = SMTPHandler(
            mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
            fromaddr='no-reply@' + app.config['MAIL_SERVER'],
            toaddrs=app.config['ADMINS'],
            subject='Microblog Failure',
            credentials=auth,
            secure=secure)
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)

        if not os.path.exists('logs'):
            os.mkdir('logs')
        file_handler = RotatingFileHandler('logs/microblog.log',
                                           maxBytes=10240,
                                           backupCount=10)
        file_handler.setFormatter(
            logging.Formatter(
                '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'
            ))
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)
示例#36
0
    def __init__(self, init_object_name):
        """Constructor for the CortexFlask application. Reads the config, sets
		up logging, configures Jinja and Flask."""

        # Call the superclass (Flask) constructor
        super(CortexFlask, self).__init__(init_object_name)

        # CSRF exemption support
        self._exempt_views = set()
        self.before_request(self._csrf_protect)

        # CSRF token function in templates
        self.jinja_env.globals['csrf_token'] = self._generate_csrf_token
        self.jinja_env.globals['utcnow'] = datetime.datetime.utcnow

        # Load the __init__.py config defaults
        self.config.from_object("cortex.defaultcfg")

        # Load system config file
        self.config.from_pyfile('/data/cortex/cortex.conf')

        # Make TEMPLATES_AUTO_RELOAD work (we've touch the Jinja environment). See resolved Flask issue #1907
        if 'TEMPLATES_AUTO_RELOAD' in self.config and self.config[
                'TEMPLATES_AUTO_RELOAD']:
            self.jinja_env.auto_reload = True

        # Set up logging to file
        if self.config['FILE_LOG'] == True:
            file_handler = RotatingFileHandler(
                self.config['LOG_DIR'] + '/' + self.config['LOG_FILE'], 'a',
                self.config['LOG_FILE_MAX_SIZE'],
                self.config['LOG_FILE_MAX_FILES'])
            file_handler.setFormatter(
                logging.Formatter('%(asctime)s %(levelname)s: %(message)s'))
            self.logger.addHandler(file_handler)

        # Set up the max log level
        if self.debug:
            self.logger.setLevel(logging.DEBUG)
            file_handler.setLevel(logging.DEBUG)
        else:
            self.logger.setLevel(logging.INFO)
            file_handler.setLevel(logging.INFO)

        # Output some startup info
        self.logger.info('cortex version ' + self.config['VERSION'] +
                         ' initialised')
        self.logger.info('cortex debug status: ' + str(self.config['DEBUG']))

        # set up e-mail alert logging
        if self.config['EMAIL_ALERTS'] == True:
            # Log to file where e-mail alerts are going to
            self.logger.info(
                'cortex e-mail alerts are enabled and being sent to: ' +
                str(self.config['ADMINS']))

            # Create the mail handler
            mail_handler = SMTPHandler(self.config['SMTP_SERVER'],
                                       self.config['EMAIL_FROM'],
                                       self.config['ADMINS'],
                                       self.config['EMAIL_SUBJECT'])

            # Set the minimum log level (errors) and set a formatter
            mail_handler.setLevel(logging.ERROR)
            mail_handler.setFormatter(
                Formatter("""
A fatal error occured in Cortex.

Message type:       %(levelname)s
Location:           %(pathname)s:%(lineno)d
Module:             %(module)s
Function:           %(funcName)s
Time:               %(asctime)s
Logger Name:        %(name)s
Process ID:         %(process)d

Further Details:

%(message)s

"""))

            self.logger.addHandler(mail_handler)

        # Debug Toolbar
        if self.config['DEBUG_TOOLBAR']:
            self.debug = True
            from flask_debugtoolbar import DebugToolbarExtension
            toolbar = DebugToolbarExtension(self)
            self.logger.info(
                'cortex debug toolbar enabled - DO NOT USE THIS ON LIVE SYSTEMS!'
            )

        # check the database is up and is working
        self.init_database()

        # set up permissions
        self.init_permissions()
示例#37
0
文件: __init__.py 项目: e2jk/syncboom
def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    db.init_app(app)
    migrate.init_app(app, db)
    login.init_app(app)
    mail.init_app(app)
    bootstrap.init_app(app)
    moment.init_app(app)
    babel.init_app(app)
    cache.init_app(app)
    app.redis = Redis.from_url(app.config['REDIS_URL'])
    app.task_queue = rq.Queue('syncboom-tasks', connection=app.redis)
    paranoid = Paranoid(app)
    paranoid.redirect_view = '/'

    app.before_request(app_before_request)

    from app.errors import bp as errors_bp
    app.register_blueprint(errors_bp)

    from app.auth import bp as auth_bp
    app.register_blueprint(auth_bp, url_prefix='/auth')

    from app.main import bp as main_bp
    app.register_blueprint(main_bp)

    from app.mapping import bp as mapping_bp
    app.register_blueprint(mapping_bp, url_prefix='/mapping')

    if not app.debug and not app.testing:
        if app.config['MAIL_SERVER']:
            auth = None
            if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
                auth = (app.config['MAIL_USERNAME'],
                        app.config['MAIL_PASSWORD'])
            secure = None
            if app.config['MAIL_USE_TLS']:
                secure = ()
            mail_handler = SMTPHandler(
                mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
                fromaddr='no-reply@' + app.config['MAIL_SERVER'],
                toaddrs=app.config['ADMINS'],
                subject='[SyncBoom] Failure',
                credentials=auth,
                secure=secure)
            mail_handler.setLevel(logging.ERROR)
            app.logger.addHandler(mail_handler)

        if app.config['LOG_TO_STDOUT']:
            stream_handler = logging.StreamHandler()
            stream_handler.setLevel(logging.INFO)
            app.logger.addHandler(stream_handler)
        else:
            if not os.path.exists('logs'):
                os.mkdir('logs')
            file_handler = RotatingFileHandler('logs/syncboom.log',
                                               maxBytes=10240,
                                               backupCount=10)
            file_handler.setFormatter(
                logging.Formatter('%(asctime)s %(levelname)s: %(message)s '
                                  '[in %(pathname)s:%(lineno)d]'))
            file_handler.setLevel(logging.INFO)
            app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)

    # Serve local Bootstrap files when in debug mode
    app.config["BOOTSTRAP_SERVE_LOCAL"] = app.debug

    @app.after_request
    def set_secure_headers(response):
        secure_headers.flask(response)
        return response

    return app
示例#38
0
def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)
    db.init_app(app)
    migrate.init_app(app, db)
    login.init_app(app)
    mail.init_app(app)
    bootstrap.init_app(app)
    moment.init_app(app)

    from app.errors import bp as errors_bp
    app.register_blueprint(errors_bp)

    from app.auth import bp as auth_bp
    app.register_blueprint(auth_bp, url_prefix='/auth')

    from app.main import bp as main_bp
    app.register_blueprint(main_bp)

    if app.debug and not app.testing:
        if app.config['MAIL_SERVER']:
            auth = None
            if app.config['MAIL_SERVER'] or app.config['MAIL_PASSWORD']:
                auth = (app.config['MAIL_USERNAME'],
                        app.config['MAIL_PASSWORD'])
            secure = None
            if app.config['MAIL_USE_TLS']:
                secure = ()
            mail_handler = SMTPHandler(mailhost=(app.config['MAIL_SERVER'],
                                                 app.config['MAIL_PORT']),
                                       fromaddr=app.config['MAIL_USERNAME'],
                                       toaddrs=app.config['ADMINS'],
                                       subject='buerry',
                                       credentials=auth,
                                       secure=secure)
            mail_handler.setFormatter(
                logging.Formatter('''
            Message type:        %(levelname)s
            Location:            %(pathname)s:%(lineno)d
            Module:              %(module)s
            Function:            %(funcName)s
            Time:                %(asctime)s
            
            Message:
            
            %(message)s
            '''))
            mail_handler.setLevel(logging.ERROR)
            app.logger.addHandler(mail_handler)

        # 设置文件日志 FileHandler
        if not os.path.exists('logs'):
            os.mkdir('logs')
        file_handler = RotatingFileHandler('logs/buerry.log',
                                           maxBytes=20240,
                                           backupCount=10,
                                           encoding='utf-8')
        file_handler.setFormatter(
            logging.Formatter(
                '%(asctime)s %(levelname)s: %(message)s [in %(filename)s:%(lineno)d]'
            ))
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)

        # 设置默认日志
        app.logger.setLevel(logging.INFO)
        # app.logger.info('蓝莓网项目启动')

    return app
示例#39
0
def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    db.init_app(app)
    migrate.init_app(app, db)
    login.init_app(app)
    mail.init_app(app)
    bootstrap.init_app(app)

    from app.auth import bp as auth_bp
    app.register_blueprint(auth_bp, url_prefix='/auth')

    from app.errors import bp as errors_bp
    app.register_blueprint(errors_bp)

    from app.main import bp as main_bp
    app.register_blueprint(main_bp)

    from app.gr_stats import bp as gr_stats_bp
    app.register_blueprint(gr_stats_bp)

    if not app.debug and not app.testing:
        if app.config['MAIL_SERVER']:
            auth = None
            if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
                auth = (app.config['MAIL_USERNAME'],
                        app.config['MAIL_PASSWORD'])
            secure = None
            if app.config['MAIL_USE_TLS']:
                secure = ()
            mail_handler = SMTPHandler(
                mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
                fromaddr='no-reply@' + app.config['MAIL_SERVER'],
                toaddrs=app.config['ADMINS'],
                subject='Crypto monitor Failure',
                credentials=auth,
                secure=secure)
            mail_handler.setLevel(logging.ERROR)
            app.logger.addHandler(mail_handler)

        if not os.path.exists('logs'):
            os.mkdir('logs')
        file_handler = RotatingFileHandler('logs/cryptomonitor.log',
                                           maxBytes=10240,
                                           backupCount=10)
        file_handler.setFormatter(
            logging.Formatter(
                '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'
            ))
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)
        app.logger.info('Crypto monitor startup')

    if app.config['LOG_TO_STDOUT']:
        stream_handler = logging.StreamHandler()
        stream_handler.setLevel(logging.INFO)
        app.logger.addHandler(stream_handler)
    else:
        if not os.path.exists('logs'):
            os.mkdir('logs')
        file_handler = RotatingFileHandler('logs/cryptomonitor.log',
                                           maxBytes=10240,
                                           backupCount=10)
        file_handler.setFormatter(
            logging.Formatter('%(asctime)s %(levelname)s: %(message)s '
                              '[in %(pathname)s:%(lineno)d]'))
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)

    app.logger.setLevel(logging.INFO)
    app.logger.info('Crypto monitor startup')

    return app
示例#40
0
logHandler = RotatingFileHandler('info.log', maxBytes=1000, backupCount=1)
# set the log handler level
logHandler.setLevel(logging.INFO)
# create formatter and add it to the handlers: date time - name of package - file name (module name) - function name - line number - level (error, infor,...) - message
formatter = logging.Formatter(
    '%(asctime)s - %(name)s - %(module)s - %(funcName)s - %(lineno)d- %(levelname)s - %(message)s'
)
logHandler.setFormatter(formatter)

# set the app logger level:  ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'). See http://flask.pocoo.org/docs/0.12/errorhandling/
app.logger.setLevel(logging.INFO)
app.logger.addHandler(logHandler)

#if not app.debug:
if app.config['MAIL_SERVER']:
    auth = None
    if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
        auth = (app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])
    secure = None
    if app.config['MAIL_USE_TLS']:
        secure = ()
    mail_handler = SMTPHandler(
        mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
        fromaddr='no-reply@' + app.config['MAIL_SERVER'],
        toaddrs=app.config['ADMINS'],
        subject='System Failure',
        credentials=auth,
        secure=secure)
    mail_handler.setLevel(logging.ERROR)  # change to CRITICAL later
    app.logger.addHandler(mail_handler)
示例#41
0
def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)
    app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
    db.init_app(app)
    migrate.init_app(app, db)
    login.init_app(app)
    mail.init_app(app)
    babel.init_app(app)
    UPLOAD_FOLDER_app=(app)
    app.jinja_env.add_extension('jinja2.ext.loopcontrols')


    from app.errors import bp as errors_bp
    app.register_blueprint(errors_bp)

    from app.auth import bp as auth_bp
    app.register_blueprint(auth_bp, url_prefix='/auth')

    from app.main import bp as main_bp
    app.register_blueprint(main_bp)




    if not app.debug and not app.testing:
        if app.config['MAIL_SERVER']:
            auth = None
            if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
                auth = (app.config['MAIL_USERNAME'],
                        app.config['MAIL_PASSWORD'])
            secure = None
            if app.config['MAIL_USE_TLS']:
                secure = ()
            mail_handler = SMTPHandler(
                mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
                fromaddr='no-reply@' + app.config['MAIL_SERVER'],
                toaddrs=app.config['ADMINS'], subject='Microblog Failure',
                credentials=auth, secure=secure)
            mail_handler.setLevel(logging.ERROR)
            app.logger.addHandler(mail_handler)

        if not os.path.exists('logs'):
            os.mkdir('logs')
        file_handler = RotatingFileHandler('logs/microblog.log',
                                           maxBytes=10240, backupCount=10)
        file_handler.setFormatter(logging.Formatter(
            '%(asctime)s %(levelname)s: %(message)s '
            '[in %(pathname)s:%(lineno)d]'))
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)
        app.logger.info('Timesol startup')

        if app.config['LOG_TO_STDOUT']:
            stream_handler = logging.StreamHandler()
            stream_handler.setLevel(logging.INFO)
            app.logger.addHandler(stream_handler)
        else:
            if not os.path.exists('logs'):
                os.mkdir('logs')
            file_handler = RotatingFileHandler('logs/microblog.log',
                                               maxBytes=10240, backupCount=10)
            file_handler.setFormatter(logging.Formatter(
                '%(asctime)s %(levelname)s: %(message)s '
                '[in %(pathname)s:%(lineno)d]'))
            file_handler.setLevel(logging.INFO)
            app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)
        app.logger.info('Timesol startup')

    return app
示例#42
0
def create_app(config_name='default'):
    """
    Set up the Flask Application context.

    :param config_name: Configuration for specific application context.

    :return: Flask application
    """

    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    # TODO: handler_info, handler_debug, handler_warn
    mail_handler = SMTPHandler(mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
                               fromaddr=app.config['MAIL_SENDER'],
                               toaddrs=app.config['ERROR_RECIPIENTS'], subject='OpenRecords Error')
    mail_handler.setLevel(logging.ERROR)
    mail_handler.setFormatter(Formatter('''
    Message Type:       %(levelname)s
    Location:           %(pathname)s:%(lineno)d
    Module:             %(module)s
    Function:           %(funcName)s
    Time:               %(asctime)s
    
    Message:
    %(message)s
    '''))
    app.logger.addHandler(mail_handler)

    handler_error = TimedRotatingFileHandler(
        os.path.join(app.config['LOGFILE_DIRECTORY'],
                     'openrecords_{}_error.log'.format(app.config['APP_VERSION_STRING'])),
        when='midnight', interval=1, backupCount=60)
    handler_error.setLevel(logging.ERROR)
    handler_error.setFormatter(Formatter(
        '------------------------------------------------------------------------------- \n'
        '%(asctime)s %(levelname)s: %(message)s '
        '[in %(pathname)s:%(lineno)d]\n'
    ))
    app.logger.addHandler(handler_error)

    app.jinja_env.filters['format_event_type'] = jinja_filters.format_event_type
    app.jinja_env.filters['format_response_type'] = jinja_filters.format_response_type
    app.jinja_env.filters['format_response_privacy'] = jinja_filters.format_response_privacy
    app.jinja_env.filters['format_ultimate_determination_reason'] = jinja_filters.format_ultimate_determination_reason

    bootstrap.init_app(app)
    es.init_app(app,
                use_ssl=app.config['ELASTICSEARCH_USE_SSL'],
                verify_certs=app.config['ELASTICSEARCH_VERIFY_CERTS'])
    db.init_app(app)
    csrf.init_app(app)
    moment.init_app(app)
    login_manager.init_app(app)
    mail.init_app(app)
    celery.conf.update(app.config)
    celery.config_from_object(celery_config)
    sentry.init_app(app, logging=app.config["USE_SENTRY"], level=logging.INFO)

    with app.app_context():
        from app.models import Anonymous
        login_manager.login_view = 'auth.login'
        login_manager.anonymous_user = Anonymous
        if app.config['USE_SAML']:
            login_manager.login_message = None
            login_manager.login_message_category = None
        KVSessionExtension(session_redis, app)

    # Error Handlers
    @app.errorhandler(400)
    def bad_request(e):
        return render_template("error/generic.html", status_code=400,
                               message=e.description or None)

    @app.errorhandler(403)
    def forbidden(e):
        return render_template("error/generic.html", status_code=403)

    @app.errorhandler(404)
    def page_not_found(e):
        return render_template("error/generic.html", status_code=404)

    @app.errorhandler(500)
    def internal_server_error(e):
        error_id = str(uuid.uuid4())
        app.logger.error("""Request:   {method} {path}
    IP:        {ip}
    User:      {user}
    Agent:     {agent_platform} | {agent_browser} {agent_browser_version}
    Raw Agent: {agent}
    Error ID:  {error_id}
            """.format(
            method=flask_request.method,
            path=flask_request.path,
            ip=flask_request.remote_addr,
            agent_platform=flask_request.user_agent.platform,
            agent_browser=flask_request.user_agent.browser,
            agent_browser_version=flask_request.user_agent.version,
            agent=flask_request.user_agent.string,
            user=current_user,
            error_id=error_id
        ), exc_info=e
        )
        return render_template("error/generic.html",
                               status_code=500,
                               error_id=error_id)

    @app.errorhandler(503)
    def maintenance(e):
        with open(os.path.join(app.instance_path, 'maintenance.json')) as f:
            maintenance_info = json.load(f)
        return render_template('error/maintenance.html',
                               description=maintenance_info['description'],
                               outage_time=maintenance_info['outage_time'])

    @app.before_request
    def check_maintenance_mode():
        if os.path.exists(os.path.join(app.instance_path, 'maintenance.json')):
            if not flask_request.cookies.get('authorized_maintainer', None):
                return abort(503)

    @app.context_processor
    def add_session_config():
        """Add current_app.permanent_session_lifetime converted to milliseconds
        to context. The config variable PERMANENT_SESSION_LIFETIME is not
        used because it could be either a timedelta object or an integer
        representing seconds.
        """
        return {
            'PERMANENT_SESSION_LIFETIME_MS': (
                    app.permanent_session_lifetime.seconds * 1000),
        }

    @app.context_processor
    def add_debug():
        """Add current_app.debug to context."""
        return dict(debug=app.debug)

    # Register Blueprints
    from .main import main
    app.register_blueprint(main)

    from .auth import auth
    app.register_blueprint(auth, url_prefix="/auth")

    from .request import request
    app.register_blueprint(request, url_prefix="/request")

    from .request.api import request_api_blueprint
    app.register_blueprint(request_api_blueprint,
                           url_prefix="/request/api/v1.0")

    from .report import report
    app.register_blueprint(report, url_prefix="/report")

    from .response import response
    app.register_blueprint(response, url_prefix="/response")

    from .upload import upload
    app.register_blueprint(upload, url_prefix="/upload")

    from .user import user
    app.register_blueprint(user, url_prefix="/user")

    from .agency import agency
    app.register_blueprint(agency, url_prefix="/agency")

    from .agency.api import agency_api_blueprint
    app.register_blueprint(agency_api_blueprint, url_prefix="/agency/api/v1.0")

    from .search import search
    app.register_blueprint(search, url_prefix="/search")

    from .admin import admin
    app.register_blueprint(admin, url_prefix="/admin")

    from .user_request import user_request
    app.register_blueprint(user_request, url_prefix="/user_request")

    from .permissions import permissions
    app.register_blueprint(permissions, url_prefix="/permissions/api/v1.0")

    return app
示例#43
0
def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    app.config['PROPAGATE_EXCEPTIONS'] = True

    def format_datetime(value, format="%B %d %Y"):
        """Format a date time to (Default): Month d YYYY HH:MM P"""
        if value is None:
            return ""
            return value.strftime(format)

    app.jinja_env.filters['datetime'] = format_datetime

    db.init_app(app)
    migrate.init_app(app, db)
    login.init_app(app)
    mail.init_app(app)
    bootstrap.init_app(app)
    moment.init_app(app)

    from app.errors import bp as errors_bp
    app.register_blueprint(errors_bp)

    from app.auth import bp as auth_bp
    app.register_blueprint(auth_bp, url_prefix='/auth')

    from app.main import bp as main_bp
    app.register_blueprint(main_bp)

    if not app.debug and not app.testing:
        if app.config['MAIL_SERVER']:
            auth = None
            if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
                auth = (app.config['MAIL_USERNAME'],
                        app.config['MAIL_PASSWORD'])
            secure = None
            if app.config['MAIL_USE_TLS']:
                secure = ()
            mail_handler = SMTPHandler(
                mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
                fromaddr='no-reply@' + app.config['MAIL_SERVER'],
                toaddrs=app.config['ADMINS'],
                subject='LLTRK Failure',
                credentials=auth,
                secure=secure)
            mail_handler.setLevel(logging.ERROR)
            app.logger.addHandler(mail_handler)

        if app.config['LOG_TO_STDOUT']:
            stream_handler = logging.StreamHandler()
            stream_handler.setLevel(logging.INFO)
            app.logger.addHandler(stream_handler)
        else:
            if not os.path.exists('logs'):
                os.mkdir('logs')
            file_handler = RotatingFileHandler('logs/LLTRK.log',
                                               maxBytes=10240,
                                               backupCount=10)
            file_handler.setFormatter(
                logging.Formatter('%(asctime)s %(levelname)s: %(message)s '
                                  '[in %(pathname)s:%(lineno)d]'))
            file_handler.setLevel(logging.INFO)
            app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)
        app.logger.info('LL Track')

    return app
示例#44
0
def configure_logging(app):
    """
    配置日志
    :param app:  app实例
    :return:
    """
    if app.config.get('TESTING', None):  # 运行测试的时候不配置日志
        return

    logs_folder = os.path.join(app.root_path, os.pardir, "logs")
    from logging.handlers import SMTPHandler
    formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s '
                                  '[in %(pathname)s:%(lineno)d]')

    # 普通信息日志
    info_log = os.path.join(logs_folder, app.config['INFO_LOG'])
    info_file_handler = logging.handlers.RotatingFileHandler(info_log,
                                                             maxBytes=100000,
                                                             backupCount=10)
    info_file_handler.setLevel(logging.INFO)
    info_file_handler.setFormatter(formatter)
    app.logger.addHandler(info_file_handler)

    # 错误日志
    error_log = os.path.join(logs_folder, app.config['ERROR_LOG'])
    error_file_handler = logging.handlers.RotatingFileHandler(error_log,
                                                              maxBytes=100000,
                                                              backupCount=10)
    error_file_handler.setLevel(logging.ERROR)
    error_file_handler.setFormatter(formatter)
    app.logger.addHandler(error_file_handler)

    # 慢查询日志
    slow_query_log = os.path.join(logs_folder, app.config['SLOW_QUERY_LOG'])
    slow_query_file_handler = logging.handlers.RotatingFileHandler(
        slow_query_log, maxBytes=100000, backupCount=10)
    slow_query_file_handler.setLevel(logging.WARN)
    slow_query_file_handler.setFormatter(formatter)
    app.logger.addHandler(slow_query_file_handler)

    # 调试日志
    if app.debug:
        debug_log = os.path.join(logs_folder, app.config['DEBUG_LOG'])
        debug_file_handler = logging.handlers.RotatingFileHandler(
            debug_log, maxBytes=100000, backupCount=10)
        debug_file_handler.setLevel(logging.DEBUG)
        debug_file_handler.setFormatter(formatter)
        app.logger.addHandler(debug_file_handler)

    if app.config["SEND_LOGS"]:
        mail_handler = \
            SMTPHandler(
                app.config['MAIL_SERVER'],
                app.config['MAIL_DEFAULT_SENDER'],
                app.config['ADMINS'],
                'application error, no admins specified',
                (app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])
            )

        mail_handler.setLevel(logging.ERROR)
        mail_handler.setFormatter(formatter)
        app.logger.addHandler(mail_handler)

    if app.config["SQLALCHEMY_ECHO"]:
        # Ref: http://stackoverflow.com/a/8428546
        @event.listens_for(Engine, "before_cursor_execute")
        def before_cursor_execute(conn, cursor, statement, parameters, context,
                                  executemany):
            conn.info.setdefault('query_start_time', []).append(time.time())

        @event.listens_for(Engine, "after_cursor_execute")
        def after_cursor_execute(conn, cursor, statement, parameters, context,
                                 executemany):
            total = time.time() - conn.info['query_start_time'].pop(-1)
            app.logger.debug("Total Time: %f", total)
            except NameError:
                return str(obj)  # python 3
        return super(CustomJSONEncoder, self).default(obj)


app.json_encoder = CustomJSONEncoder

if not app.debug:
    import logging
    from logging.handlers import SMTPHandler
    credentials = None
    if MAIL_USERNAME or MAIL_PASSWORD:
        credentials = (MAIL_USERNAME, MAIL_PASSWORD)
    mailhandl = SMTPHandler((MAIL_SERVER, MAIL_PORT), 'no rply@' + MAIL_SERVER,
                            ADMINS, 'blog failure', credentials)
    mailhandl.setLevel(logging.ERROR)
    app.logger.addHandler(mailhandl)

if not app.debug:
    import logging
    from logging.handlers import RotatingFileHandler
    file_handler = RotatingFileHandler('tmp/microblog.log', 'a',
                                       1 * 1024 * 1024, 10)
    file_handler.setFormatter(
        logging.Formatter(
            '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'
        ))
    app.logger.setLevel(logging.INFO)
    file_handler.setLevel(logging.INFO)
    app.logger.addHandler(file_handler)
    app.logger.info('microblog startup')
示例#46
0
dictConfig(app.config['LOG_CONFIG'])

if not app.config['DEBUG']:
    import logging
    from logging.handlers import SMTPHandler
    credentials = None
    if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
        credentials = (app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])
    mail_handler = SMTPHandler(
        (app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
        app.config['MAIL_DEFAULT_SENDER'][1],
        app.config['ADMINS'],
        'App Error Message',
        credentials
    )
    mail_handler.setLevel(logging.DEBUG)
    app.logger.addHandler(mail_handler)


# 这个 import 语句放在这里, 防止views, models import发生循环import
from app_backend import models, tasks

# 导入视图(不使用蓝图的单模式方式)
from app_backend import views

# 导入视图(不使用蓝图的多模块方式)
# from application.views import auth
# from application.views import blog
# from application.views import reg
# from application.views import site
# from application.views import user
示例#47
0
def create_app(config_class=Config):
    # Create an app object as an instance of Flask class.
    # __name__ is a special Python variable, set to the name of the module
    # in which it is invoked (in this case, app). Flask will use the location of the module
    # as a starting point when needing to load resources.
    # This is almost always the way to start.
    app = Flask(__name__)
    # Add config items to the Flask app object, which can be
    # accessed via a dictionary
    app.config.from_object(config_class)

    # Init the exensions here now that app is created
    db.init_app(app)
    migrate.init_app(app, db)
    login.init_app(app)
    mail.init_app(app)
    bootstrap.init_app(app)
    moment.init_app(app)
    babel.init_app(app)

    # Register blueprints. Import directly above to avoid circular imports.
    from app.errors import bp as errors_bp
    app.register_blueprint(errors_bp)

    # URL prefix kinda like a namespace
    from app.auth import bp as auth_bp
    app.register_blueprint(auth_bp, url_prefix='/auth')

    from app.main import bp as main_bp
    app.register_blueprint(main_bp)

    # Elasticsearch isn't a Flask extension, so we can't create an
    # instance of it. Instead, create a new attribute (a little hacky)
    app.elasticsearch = Elasticsearch([app.config['ELASTICSEARCH_URL']]) \
        if app.config['ELASTICSEARCH_URL'] else None

    # Background tasks manager initialization. This is better than threads.
    app.redis = Redis.from_url(app.config['REDIS_URL'])
    app.task_queue = rq.Queue('microblog-tasks', connection=app.redis)

    # Register the API blueprint
    from app.api import bp as api_bp
    app.register_blueprint(api_bp, url_prefix='/api')

    if not app.debug and not app.testing:
        # Do email notifcaton of errors
        if app.config['MAIL_SERVER']:
            auth = None
            if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
                auth = (app.config['MAIL_USERNAME'],
                        app.config['MAIL_PASSWORD'])
            secure = None
            if app.config['MAIL_USE_TLS']:
                secure = ()
            mail_handler = SMTPHandler(
                mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
                fromaddr='no-reply@' + app.config['MAIL_SERVER'],
                toaddrs=app.config['ADMINS'],
                subject='Microblog-Py Failure',
                credentials=auth,
                secure=secure)
            mail_handler.setLevel(logging.ERROR)
            app.logger.addHandler(mail_handler)

        # Log file setup
        # This log option handles when they are printed to terminal,
        # like on Heroku
        if app.config['LOG_TO_STDOUT']:
            stream_handler = logging.StreamHandler()
            stream_handler.setLevel(logging.INFO)
            app.logger.addHandler(stream_handler)
        else:
            # Write logs to file on disk
            if not os.path.exists('logs'):
                os.mkdir('logs')
            file_handler = RotatingFileHandler('logs/microblog.log',
                                               maxBytes=10240,
                                               backupCount=10)
            file_handler.setFormatter(
                logging.Formatter(
                    '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'
                ))
            file_handler.setLevel(logging.INFO)
            app.logger.addHandler(file_handler)

        app.logger.addHandler(logging.INFO)
        # app.logger.info('Microblog-Py startup')

    return app
示例#48
0
                               config.admins,
                               '[faucet] Error',
                               (config.mail_user,
                                config.mail_pass))
log_handler_mail.setFormatter(logging.Formatter(
    "Message type:       %(levelname)s\n" +
    "Location:           %(pathname)s:%(lineno)d\n" +
    "Module:             %(module)s\n" +
    "Function:           %(funcName)s\n" +
    "Time:               %(asctime)s\n" +
    "\n" +
    "Message:\n" +
    "\n" +
    "%(message)s\n"
))
log_handler_mail.setLevel(logging.WARN)
log_handler_stdout = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
log_handler_stdout.setFormatter(formatter)
log_handler_rotate = RotatingFileHandler('faucet.log',
                                         maxBytes=1024 * 1024 * 100,
                                         backupCount=20)
log_handler_rotate.setLevel(logging.CRITICAL)
app.logger.addHandler(log_handler_mail)
app.logger.addHandler(log_handler_rotate)
app.logger.addHandler(log_handler_stdout)

# Load views and models
from . import views, models

示例#49
0
def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    db.init_app(app)
    migrate.init_app(app, db)
    login.init_app(app)
    mail.init_app(app)
    bootstrap.init_app(app)
    moment.init_app(app)
    babel.init_app(app)

    app.elasticsearch = Elasticsearch([app.config['ELASTICSEARCH_URL']]) \
        if app.config['ELASTICSEARCH_URL'] else None

    from app.errors import bp as errors_bp
    app.register_blueprint(errors_bp)

    from app.auth import bp as auth_bp
    app.register_blueprint(auth_bp, url_prefix='/auth')

    from app.main import bp as main_bp
    app.register_blueprint(main_bp)

    app.redis = Redis.from_url(app.config['REDIS_URL'])
    app.task_queue = rq.Queue('microblog-tasks', connection=app.redis)

    if not app.debug and not app.testing:
        if app.config['MAIL_SERVER']:
            auth = None
            if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
                auth = (app.config['MAIL_USERNAME'],
                        app.config['MAIL_PASSWORD'])
            secure = None
            if app.config['MAIL_USE_TLS']:
                secure = ()
            mail_handler = SMTPHandler(
                mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
                fromaddr='no-reply@' + app.config['MAIL_SERVER'],
                toaddrs=app.config['ADMINS'],
                subject='Microblog Failure',
                credentials=auth,
                secure=secure)
            mail_handler.setLevel(logging.ERROR)
            app.logger.addHandler(mail_handler)

        if os.path.exists('.flaskenv'):
            with open('.flaskenv') as file:
                line = file.readline().strip()
                current_directory = line[line.find('=') +
                                         1:line.find('/')] + '/'
        elif os.getenv('FLASK_APP') is not None:
            current_directory = os.environ['FLASK_APP'].split('/')[0] + '/'
        else:
            current_directory = ''
        if not os.path.exists(current_directory + 'logs'):
            os.mkdir(current_directory + 'logs')
        file_handler = RotatingFileHandler(current_directory +
                                           'logs/microblog.log',
                                           maxBytes=10240,
                                           backupCount=10)
        file_handler.setFormatter(
            logging.Formatter('%(asctime)s %(levelname)s: %(message)s '
                              '[in %(pathname)s:%(lineno)d]'))
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)
        app.logger.info('Microblog startup')

    return app
示例#50
0
def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    db.init_app(app)
    migrate.init_app(app, db)
    login_manager.init_app(app)

    from companyblog.core.views import core
    from companyblog.error_pages.handlers import error_pages
    from companyblog.users.views import users
    from companyblog.blog_posts.views import blog_posts

    app.register_blueprint(core)
    app.register_blueprint(error_pages)
    app.register_blueprint(users)
    app.register_blueprint(blog_posts)

    app.cli.add_command(create_tables)

    if not app.debug:
        # sends errors to mail
        if app.config['MAIL_SERVER']:
            auth = None
            if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
                auth = (app.config['MAIL_USERNAME'],
                        app.config['MAIL_PASSWORD'])
            secure = None
            if app.config['MAIL_USE_TLS']:
                secure = ()
            mail_handler = SMTPHandler(
                mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
                fromaddr='no-reply@' + app.config['MAIL_SERVER'],
                toaddrs=app.config['ADMINS'],
                subject='Companyblog Failure',
                credentials=auth,
                secure=secure)
            mail_handler.setLevel(logging.ERROR)
            app.logger.addHandler(mail_handler)

        if app.config['LOG_TO_STDOUT']:
            stream_handler = logging.StreamHandler()
            stream_handler.setLevel(logging.INFO)
            app.logger.addHandler(stream_handler)
        else:
            # sets up logging to file
            if not os.path.exists('logs'):
                os.mkdir('logs')
            file_handler = RotatingFileHandler('logs/companyblog.log',
                                               maxBytes=10240,
                                               backupCount=10)
            file_handler.setFormatter(
                logging.Formatter(
                    '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'
                ))
            file_handler.setLevel(logging.INFO)
            app.logger.addHandler(file_handler)

            app.logger.setLevel(logging.INFO)
            app.logger.info('Company Blog')

        return app
示例#51
0
def create_app(config_name='default'):

    app = Flask(__name__)
    app.config.from_object(config[config_name])

    db.init_app(app)
    migrate.init_app(app, db)
    login.init_app(app)
    mail.init_app(app)
    moment.init_app(app)

    celery.conf.update(app.config)

    app.elasticsearch = Elasticsearch([
        app.config['ELASTICSEARCH_URL']
    ]) if app.config['ELASTICSEARCH_URL'] else None

    assets.init_app(app)
    bundles = {
        'globals_css':
        Bundle('src/css/styles.css',
               output='css/globals.css',
               filters="postcss"),
        'utils_css':
        Bundle('src/css/upvotejs.css',
               'node_modules/cropperjs/dist/cropper.min.css',
               output='css/utils.css'),
        'bp_css':
        Bundle('src/css/sidebar_style.css',
               output='css/bp.css',
               filters='postcss'),
        'minigame_css':
        Bundle('src/minigame/*.css', output='css/minigame.css'),
        'globals_js':
        Bundle('node_modules/jquery/dist/jquery.min.js',
               'node_modules/@popperjs/core/dist/umd/popper.min.js',
               'node_modules/htmx.org/dist/htmx.min.js',
               output='js/globals.js',
               filters='jsmin'),
        'utils_js':
        Bundle('node_modules/cropperjs/dist/cropper.min.js',
               'src/js/upvotejs.jquery.js',
               'src/js/upvotejs.js',
               output='js/utils.js',
               filters='jsmin'),
        'bp_js':
        Bundle('src/js/edit_profile.js', output='js/bp.js', filters='jsmin'),
        'minigame_js':
        Bundle('src/minigame/*.js', output='js/minigame.js', filters='jsmin'),
    }
    assets.register(bundles)

    from app.errors import bp as errors_bp
    app.register_blueprint(errors_bp)

    from app.auth import bp as auth_bp
    app.register_blueprint(auth_bp, url_prefix='/auth')

    from app.utils import bp as utils_bp
    app.register_blueprint(utils_bp)

    from app.main import bp as main_bp
    app.register_blueprint(main_bp)

    from app.settings import bp as settings_bp
    app.register_blueprint(settings_bp, url_prefix='/settings')

    from app.blog import bp as blog_bp
    app.register_blueprint(blog_bp, url_prefix='/blog')

    from app.explore import bp as explore_bp
    app.register_blueprint(explore_bp)

    if not app.debug and not app.testing:
        if app.config['MAIL_SERVER']:
            auth = None
            if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
                auth = (app.config['MAIL_USERNAME'],
                        app.config['MAIL_PASSWORD'])
            secure = None
            if app.config['MAIL_USE_TLS']:
                secure = ()
            mail_handler = SMTPHandler(
                mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
                fromaddr='no-reply@' + app.config['MAIL_SERVER'],
                toaddrs=app.config['ADMINS'],
                subject='Microblog Failure',
                credentials=auth,
                secure=secure)
            mail_handler.setLevel(logging.ERROR)
            app.logger.addHandler(mail_handler)
        if not os.path.exists('logs'):
            os.mkdir('logs')
        file_handler = RotatingFileHandler('logs/microblog.log',
                                           maxBytes=10240,
                                           backupCount=10)
        file_handler.setFormatter(
            logging.Formatter(
                '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'
            ))
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)
        sys.stdout = StreamToLogger(app.logger, logging.INFO)
        app.logger.setLevel(logging.INFO)
        app.logger.info('Microblog startup')

    return app
示例#52
0
    def _init_logging(self):

        ## Set up logging to file
        if self.config['FILE_LOG'] == True:
            self.file_handler = RotatingFileHandler(
                self.config['LOG_DIR'] + '/' + self.config['LOG_FILE'], 'a',
                self.config['LOG_FILE_MAX_SIZE'],
                self.config['LOG_FILE_MAX_FILES'])
            self.file_handler.setFormatter(
                logging.Formatter('%(asctime)s %(levelname)s: %(message)s'))
            self.logger.addHandler(self.file_handler)

        ## Set the log level based on debug flag
        if self.debug:
            self.logger.setLevel(logging.DEBUG)
            self.file_handler.setLevel(logging.DEBUG)
        else:
            self.logger.setLevel(logging.INFO)
            self.file_handler.setLevel(logging.INFO)

        ## Output some startup info
        self.logger.info('bargate version ' + self.config['VERSION'] +
                         ' starting')
        if self.debug:
            self.logger.info('bargate is running in DEBUG mode')

        ## Log if the app is disabled at startup
        if self.config['DISABLE_APP']:
            self.logger.info('bargate is running in DISABLED mode')

        # set up e-mail alert logging
        if self.config['EMAIL_ALERTS'] == True:
            ## Log to file where e-mail alerts are going to
            self.logger.info(
                'fatal errors will generate e-mail alerts and will be sent to: '
                + str(self.config['ADMINS']))

            ## Create the mail handler
            smtp_handler = SMTPHandler(self.config['SMTP_SERVER'],
                                       self.config['EMAIL_FROM'],
                                       self.config['ADMINS'],
                                       self.config['EMAIL_SUBJECT'])

            ## Set the minimum log level (errors) and set a formatter
            smtp_handler.setLevel(logging.ERROR)
            smtp_handler.setFormatter(
                Formatter("""A fatal error occured in Bargate.

Message type:       %(levelname)s
Location:           %(pathname)s:%(lineno)d
Module:             %(module)s
Function:           %(funcName)s
Time:               %(asctime)s
Logger Name:        %(name)s
Process ID:         %(process)d

Further Details:

%(message)s

"""))

            self.logger.addHandler(smtp_handler)
示例#53
0
def create_app(config=Config()):
    my_app = Flask(__name__)
    my_app.config.from_object(config)

    #elastic search
    my_app.elastic_search = Elasticsearch([my_app.config['ELASTIC_SEARCH_URL']]) \
        if my_app.config['ELASTIC_SEARCH_URL'] else None

    # инициализация приложения
    db.init_app(my_app)
    migration.init_app(my_app, db)
    login.init_app(my_app)
    mail.init_app(my_app)
    moment.init_app(my_app)
    bootstrap.init_app(my_app)
    babel.init_app(my_app)

    # регистрация blueprint`ов
    from app.errors import bp as errors_bp
    my_app.register_blueprint(errors_bp)

    from app.auth import bp as auth_bp
    my_app.register_blueprint(auth_bp, url_prefix="/auth")

    from app.main import bp as main_bp
    my_app.register_blueprint(main_bp)

    from app.api import bp as api_bp
    my_app.register_blueprint(api_bp, url_prefix="/api")

    # для продакш-сервера: отправка ошибок по почте
    if not my_app.debug and not my_app.testing:
        if my_app.config["MAIL_SERVER"]:
            auth = None
            if my_app.config['MAIL_USERNAME'] or my_app.config['MAIL_PASSWORD']:
                auth = (my_app.config['MAIL_USERNAME'],
                        my_app.config['MAIL_PASSWORD'])
            secure = None
            if my_app.config["MAIL_USE_SSL"]:
                secure = ()
            mail_handler = SMTPHandler(mailhost=(my_app.config["MAIL_SERVER"],
                                                 my_app.config["MAIL_PORT"]),
                                       fromaddr='no-reply@' +
                                       my_app.config["MAIL_SERVER"],
                                       toaddrs=my_app.config["ADMINS"],
                                       subject='SeriousBlog failure info',
                                       credentials=auth,
                                       secure=secure)
            mail_handler.setLevel(logging.ERROR)
            my_app.logger.addHandler(mail_handler)

    # логирование в файл
    if my_app.config["LOG_TO_STDOUT"]:
        stream_h = logging.StreamHandler()
        stream_h.setLevel(logging.INFO)
        my_app.logger.addHandler(stream_h)
    else:
        if not os.path.exists('logs'):
            os.mkdir('logs')
        fh = RotatingFileHandler('logs/blog.log',
                                 maxBytes=1024 * 100,
                                 backupCount=100)
        fh.setFormatter(
            logging.Formatter(
                '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'
            ))
        fh.setLevel(logging.INFO)
        my_app.logger.addHandler(fh)

        my_app.logger.setLevel(logging.INFO)
        my_app.logger.info("SeriousBlog started")

    return my_app