Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions redash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from redash.app import create_app # noqa
from redash.destinations import import_destinations
from redash.query_runner import import_query_runners

from redash.utils.format import CustomFormatter
__version__ = "24.04.0-dev"


Expand All @@ -25,8 +25,7 @@

def setup_logging():
handler = logging.StreamHandler(sys.stdout if settings.LOG_STDOUT else sys.stderr)
formatter = logging.Formatter(settings.LOG_FORMAT)
handler.setFormatter(formatter)
handler.setFormatter(CustomFormatter())
logging.getLogger().addHandler(handler)
logging.getLogger().setLevel(settings.LOG_LEVEL)

Expand Down
7 changes: 4 additions & 3 deletions redash/authentication/org_resolving.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

from flask import g, request
from werkzeug.local import LocalProxy

from redash.models import Organization


from redash.utils.format import CustomFormatter
from redash.utils.job_filter import CurrentJobFilter
def _get_current_org():
if "org" in g:
return g.org
Expand All @@ -17,6 +16,8 @@ def _get_current_org():

g.org = Organization.get_by_slug(slug)
logging.debug("Current organization: %s (slug: %s)", g.org, slug)
CustomFormatter.org_id = g.org
CurrentJobFilter.org_id = g.org
return g.org


Expand Down
4 changes: 1 addition & 3 deletions redash/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,7 @@
RQ_WORKER_JOB_LOG_FORMAT = os.environ.get(
"REDASH_RQ_WORKER_JOB_LOG_FORMAT",
(
LOG_PREFIX + "[%(asctime)s][PID:%(process)d][%(levelname)s][%(name)s] "
"job.func_name=%(job_func_name)s "
"job.id=%(job_id)s %(message)s"
LOG_PREFIX + "%(org_id)s - %(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d) - PID:%(process)d - job.func_name=%(job_func_name)s - job.id=%(job_id)s "
),
)

Expand Down
36 changes: 36 additions & 0 deletions redash/utils/format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Module handle the console display formatting."""
import logging
from redash import settings
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Settings is being imported but not used.


class CustomFormatter(logging.Formatter):
"""Class extends the logging Formatter class to support custom colour messages."""

blue = "\x1b[34;21m"
grey = "\x1b[38;21m"
green = "\x1b[32;1m"
yellow = "\x1b[33;21m"
red = "\x1b[31;21m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
__format = (
"%(org_id)s - %(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
)

FORMATS = {
logging.DEBUG: blue + __format + reset,
logging.INFO: green + __format + reset,
logging.WARNING: yellow + __format + reset,
logging.ERROR: red + __format + reset,
logging.CRITICAL: bold_red + __format + reset,
}


org_id = "default"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to change this from a Class variable to an Instance Variables


def format(self, record):
"""Returns the formatted information."""
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)

record.org_id = CustomFormatter.org_id
return formatter.format(record)
14 changes: 14 additions & 0 deletions redash/utils/job_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

import logging
from rq import get_current_job

class CurrentJobFilter(logging.Filter):
org_id = "default"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to change this from a Class variable to an Instance Variables

def filter(self, record):
current_job = get_current_job()

record.job_id = current_job.id if current_job else ""
record.job_func_name = current_job.func_name if current_job else ""
# record.org_id = _get_current_org()
record.org_id = CurrentJobFilter.org_id
return True
13 changes: 2 additions & 11 deletions redash/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from rq import get_current_job
from rq.decorators import job as rq_job

from redash.utils.job_filter import CurrentJobFilter
from redash import rq_redis_connection, settings
#from redash.authentication.org_resolving import _get_current_org
from redash.tasks.worker import Queue as RedashQueue

default_operational_queues = ["periodic", "emails", "default"]
default_query_queues = ["scheduled_queries", "queries", "schemas"]
default_queues = default_operational_queues + default_query_queues
Expand All @@ -25,15 +25,6 @@ class StatsdRecordingJobDecorator(rq_job): # noqa
)


class CurrentJobFilter(logging.Filter):
def filter(self, record):
current_job = get_current_job()

record.job_id = current_job.id if current_job else ""
record.job_func_name = current_job.func_name if current_job else ""

return True


def get_job_logger(name):
logger = logging.getLogger("rq.job." + name)
Expand Down