##// END OF EJS Templates
setup: change url to github
setup: change url to github

File last commit:

r168:b242e5b8
r196:472d1df0 master
Show More
log.py
132 lines | 4.6 KiB | text/x-python | PythonLexer
project: initial commit
r0 # -*- coding: utf-8 -*-
license: change the license to Apache 2.0
r112 # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors
project: initial commit
r0 #
license: change the license to Apache 2.0
r112 # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
project: initial commit
r0 #
license: change the license to Apache 2.0
r112 # http://www.apache.org/licenses/LICENSE-2.0
project: initial commit
r0 #
license: change the license to Apache 2.0
r112 # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
project: initial commit
r0
import sqlalchemy as sa
import logging
import hashlib
from datetime import datetime
from appenlight.models import Base
from appenlight.lib.utils import convert_es_type
from appenlight.lib.enums import LogLevel
from sqlalchemy.dialects.postgresql import JSON
from ziggurat_foundations.models.base import BaseModel
log = logging.getLogger(__name__)
class Log(Base, BaseModel):
black: reformat source
r153 __tablename__ = "logs"
__table_args__ = {"implicit_returning": False}
project: initial commit
r0
log_id = sa.Column(sa.BigInteger(), nullable=False, primary_key=True)
black: reformat source
r153 resource_id = sa.Column(
sa.Integer(),
sa.ForeignKey(
"applications.resource_id", onupdate="CASCADE", ondelete="CASCADE"
),
nullable=False,
index=True,
)
log_level = sa.Column(sa.Unicode, nullable=False, index=True, default="INFO")
message = sa.Column(sa.UnicodeText(), default="")
timestamp = sa.Column(
sa.DateTime(), default=datetime.utcnow, server_default=sa.func.now()
)
project: initial commit
r0 request_id = sa.Column(sa.Unicode())
namespace = sa.Column(sa.Unicode())
primary_key = sa.Column(sa.Unicode())
tags = sa.Column(JSON(), default={})
permanent = sa.Column(sa.Boolean(), nullable=False, default=False)
def __str__(self):
black: reformat source
r153 return self.__unicode__().encode("utf8")
project: initial commit
r0
def __unicode__(self):
black: reformat source
r153 return "<Log id:%s, lv:%s, ns:%s >" % (
self.log_id,
self.log_level,
self.namespace,
)
project: initial commit
r0
def set_data(self, data, resource):
black: reformat source
r153 level = data.get("log_level").upper()
project: initial commit
r0 self.log_level = getattr(LogLevel, level, LogLevel.UNKNOWN)
black: reformat source
r153 self.message = data.get("message", "")
server_name = data.get("server", "").lower() or "unknown"
self.tags = {"server_name": server_name}
if data.get("tags"):
for tag_tuple in data["tags"]:
project: initial commit
r0 self.tags[tag_tuple[0]] = tag_tuple[1]
black: reformat source
r153 self.timestamp = data["date"]
r_id = data.get("request_id", "")
project: initial commit
r0 if not r_id:
black: reformat source
r153 r_id = ""
self.request_id = r_id.replace("-", "")
project: initial commit
r0 self.resource_id = resource.resource_id
black: reformat source
r153 self.namespace = data.get("namespace") or ""
self.permanent = data.get("permanent")
self.primary_key = data.get("primary_key")
project: initial commit
r0 if self.primary_key is not None:
black: reformat source
r153 self.tags["appenlight_primary_key"] = self.primary_key
project: initial commit
r0
def get_dict(self):
instance_dict = super(Log, self).get_dict()
black: reformat source
r153 instance_dict["log_level"] = LogLevel.key_from_value(self.log_level)
instance_dict["resource_name"] = self.application.resource_name
project: initial commit
r0 return instance_dict
@property
def delete_hash(self):
if not self.primary_key:
return None
black: reformat source
r153 to_hash = "{}_{}_{}".format(self.resource_id, self.primary_key, self.namespace)
return hashlib.sha1(to_hash.encode("utf8")).hexdigest()
project: initial commit
r0
def es_doc(self):
tags = {}
tag_list = []
for name, value in self.tags.items():
# replace dot in indexed tag name
black: reformat source
r153 name = name.replace(".", "_")
project: initial commit
r0 tag_list.append(name)
tags[name] = {
"values": convert_es_type(value),
black: reformat source
r153 "numeric_values": value
if (isinstance(value, (int, float)) and not isinstance(value, bool))
else None,
project: initial commit
r0 }
return {
elasticsearch: move to single doctype indices
r168 "log_id": str(self.log_id),
black: reformat source
r153 "delete_hash": self.delete_hash,
"resource_id": self.resource_id,
"request_id": self.request_id,
"log_level": LogLevel.key_from_value(self.log_level),
"timestamp": self.timestamp,
"message": self.message if self.message else "",
"namespace": self.namespace if self.namespace else "",
"tags": tags,
"tag_list": tag_list,
project: initial commit
r0 }
@property
def partition_id(self):
if self.permanent:
black: reformat source
r153 return "rcae_l_%s" % self.timestamp.strftime("%Y_%m")
project: initial commit
r0 else:
black: reformat source
r153 return "rcae_l_%s" % self.timestamp.strftime("%Y_%m_%d")