Example #1
0
def rewrite_logs(path):
    """ This is called by the site logger initialization code to migrate
    the logs from being written in site.log to being written in separate files
    """
    log.info("Migrating logs old storage to new storage for %s", path)
    to_process = []
    for name in os.listdir(path):
        if name.startswith('site.log'):
            to_process.append(name)

    entries = {}

    for name in to_process:
        fname = os.path.join(path, name)
        with open(fname) as f:
            counter = 0
            for line in f:
                counter += 1
                try:
                    data = json.loads(line)
                except:
                    log.warning("Line %s could not be parsed in file %s" % (
                        counter, fname))
                    continue
                date = data['asctime']
                ident = date[:5]  # date looks like: "12-09-17 17:02:49,451019"
                if not ident in entries:
                    entries[ident] = []

                entries[ident].append(line)

    for ident in entries.keys():
        flog = os.path.join(path, "splitlog-" + ident + '.log')
        with open(flog, 'w') as _log:
            _log.writelines(entries[ident])
Example #2
0
def get_site_logger_content(site, REQUEST=None, RESPONSE=None):
    """
    Returns plain text and parsed lines of logging files for actions on
    content

    """
    lines = []
    plain_text_lines = []
    show_plain_text = False
    writeable = False
    abs_path = get_log_dir(site)
    log_filename = os.path.join(abs_path, LOG_FILENAME)
    if abs_path and os.path.exists(log_filename) and os.access(
            log_filename, os.W_OK):
        writeable = True
        log_file = open(log_filename)
        file_len = file_length(log_filename)

        if file_len < 200:
            show_plain_text = True

        for line in log_file:
            if show_plain_text:
                plain_text_lines.append(line)

            line = json.loads(line)
            line_data = line['message']
            date_str = line['asctime']
            time_tuple = time.strptime(date_str, "%y-%m-%d %H:%M:%S,%f")
            line_data['date'] = datetime(*(time_tuple[0:6]))
            line_data['readable_message'] = readable_action(line_data)
            lines.append(line_data)

    return {
        'writeable': writeable,
        'lines': lines,
        'plain_text_lines': plain_text_lines,
    }
Example #3
0
def get_site_logger_content(site, REQUEST=None, RESPONSE=None):
    """
    Returns plain text and parsed lines of logging files for actions on
    content

    """
    lines = []
    plain_text_lines = []
    show_plain_text = False
    writeable = False
    abs_path = get_log_dir(site)
    log_filename = os.path.join(abs_path, LOG_FILENAME)
    if abs_path and os.path.exists(log_filename) and os.access(log_filename, os.W_OK):
        writeable = True
        log_file = open(log_filename)
        file_len = file_length(log_filename)

        if file_len < 200:
            show_plain_text = True

        for line in log_file:
            if show_plain_text:
                plain_text_lines.append(line)

            line = json.loads(line)
            line_data = line['message']
            date_str = line['asctime']
            time_tuple = time.strptime(date_str, "%y-%m-%d %H:%M:%S,%f")
            line_data['date'] = datetime(*(time_tuple[0:6]))
            line_data['readable_message'] = readable_action(line_data)
            lines.append(line_data)

    return {
        'writeable': writeable,
        'lines': lines,
        'plain_text_lines': plain_text_lines,
    }
Example #4
0
def get_site_logger_content(site, REQUEST=None, RESPONSE=None, month=None):
    """
    Returns plain text and parsed lines of logging files for actions on
    content

    month is an optional string in form of "shortyear-month", ex: '13-05'
    """
    lines = []
    plain_text_lines = []
    #show_plain_text = False
    abs_path = get_log_dir(site)

    if not abs_path:
        return {
            'writeable': False,
            'lines': lines,
            'plain_text_lines': plain_text_lines,
        }

    if month:
        if month == 'current':
            today = date.today()
            month = today.strftime("%y-%m")
        log_filenames = [LOG_PREFIX + "-%s.log" % month]
    else:
        log_filenames = [
            n for n in os.listdir(abs_path) if n.startswith(LOG_PREFIX)]

    log_filenames.sort()

    lines = []
    writeables = []

    for logname in log_filenames:
        lfname = os.path.join(abs_path, logname)

        if (not os.path.exists(lfname)) and os.access(lfname, os.W_OK):
            log.error("Could not access log file %s", lfname)
            writeables.append(False)
            continue

        log_file = open(lfname)
        writeables.append(True)

        c = 0
        for line in log_file:
            c += 1
            plain_text_lines.append(line)

            try:
                line = json.loads(line)
            except json.JSONDecodeError:
                log.error("Could not parse line %s from file %s", c, lfname)
                continue

            line_data = line['message']
            date_str = line['asctime']
            time_tuple = time.strptime(date_str, "%y-%m-%d %H:%M:%S,%f")
            line_data['date'] = datetime(*(time_tuple[0:6]))
            line_data['readable_message'] = readable_action(line_data)
            lines.append(line_data)

    return {
        'writeable': all(writeables),
        'lines': lines,
        'plain_text_lines': plain_text_lines,
    }