'Application level logging. Still needs fleshing out (log levels and such)
Logging events is a config setting - if False, do not log.'

def log_event(self, message, tool_id=None, **kwargs):

    if self.app.config.log_events:
        event = self.app.model.Event()
        event.tool_id = tool_id
        try:
            event.message = (message % kwargs)
        except:
            event.message = message
        try:
            event.history = self.get_history()
        except:
            event.history = None
        try:
            event.history_id = self.history.id
        except:
            event.history_id = None
        try:
            event.user = self.user
        except:
            event.user = None
        try:
            event.session_id = self.galaxy_session.id
        except:
            event.session_id = None
        self.sa_session.add(event)
        self.sa_session.flush()
