def _write(self, msg): # all output should be encoded in utf-8 # to be correctly written to file or terminal # in presence of international characters text = force_text(msg + '\n') encoded = text.encode('utf-8') self.stream.write(encoded)
def get_log_entry(msg, timestamp, source_host, fields): return { '@message': force_text(msg.text), '@timestamp': timestamp.isoformat(), '@source_host': source_host, '@fields': fields, }
def format(self, msg, source_host=None): if source_host is None: source_host = socket.gethostname() fields = msg.fields.copy() fields['level'] = get_log_level_str(fields['level']) timestamp = fields.pop('time') timestamp = datetime.datetime.utcfromtimestamp(calendar.timegm(timestamp)) timestamp = timestamp.replace(tzinfo=pytz.utc) if msg.traceback: fields['exception'] = force_text(msg.traceback) for key, value in fields.items(): if not isinstance(value, NUMERIC_TYPES): if isinstance(value, six.string_types): fields[key] = force_text(value) else: fields[key] = six.text_type(value) return self.get_log_entry(msg, timestamp, source_host, fields)
def format(self, msg, source_host=None): if source_host is None: source_host = socket.gethostname() fields = msg.fields.copy() fields['level'] = get_log_level_str(fields['level']) timestamp = fields.pop('time') timestamp = datetime.datetime.utcfromtimestamp(calendar.timegm(timestamp)) timestamp = timestamp.replace(tzinfo=pytz.utc) if msg.traceback: fields['exception'] = force_text(msg.traceback) for key, value in fields.items(): if not isinstance(value, (int, float)): if isinstance(value, six.string_types): fields[key] = force_text(value) else: fields[key] = six.text_type(value) return self.get_log_entry(msg, timestamp, source_host, fields)
def process_item(value): if not isinstance(value, NUMERIC_TYPES): if isinstance(value, six.string_types): # encode utf-8 to unicode, if necessary return force_text(value) else: if isinstance(value, dict): return prepare_fields(value) elif isinstance(value, list): return list(map(process_item, value)) return six.text_type(value) return value
def format(self, msg, source_host=None): if source_host is None: source_host = socket.gethostname() fields = msg.fields.copy() fields['level'] = get_log_level_str(fields['level']) timestamp = fields.pop('time') timestamp = datetime.datetime.utcfromtimestamp(calendar.timegm(timestamp)) timestamp = timestamp.replace(tzinfo=pytz.utc) if msg.traceback: fields['exception'] = force_text(msg.traceback) fields = prepare_fields(fields) return self.get_log_entry(msg, timestamp, source_host, fields)
def format(self, msg, source_host=None): if source_host is None: source_host = socket.gethostname() fields = msg.fields.copy() fields['level'] = get_log_level_str(fields['level']) timestamp = fields.pop('time') timestamp = datetime.datetime.utcfromtimestamp( calendar.timegm(timestamp)) timestamp = timestamp.replace(tzinfo=pytz.utc) if msg.traceback: fields['exception'] = force_text(msg.traceback) fields = prepare_fields(fields) return self.get_log_entry(msg, timestamp, source_host, fields)
def _write(self, msg): self.stream.write(force_text(msg + '\n'))