Create custom logging

This commit is contained in:
Marvin Martinson 2021-08-11 14:47:50 +00:00
parent a3e410f3a9
commit 08c9a1c7ec
13 changed files with 65 additions and 35 deletions

View File

@ -6,9 +6,9 @@ from pinecrypt.server import authority, errors, db
from bson.objectid import ObjectId
from pinecrypt.server.decorators import csrf_protection
from .utils.firewall import login_required, authorize_admin
from pinecrypt.server.mongolog import LogHandler
logger = logging.getLogger(__name__)
logger = LogHandler()
class DisableEnableAccessToInstance(object):
@csrf_protection

View File

@ -3,8 +3,9 @@ import logging
from pinecrypt.server import authority, const, config
from pinecrypt.server.common import cert_to_dn
from pinecrypt.server.decorators import serialize
from pinecrypt.server.mongolog import LogHandler
logger = logging.getLogger(__name__)
logger = LogHandler()
class BootstrapResource(object):
@serialize

View File

@ -8,11 +8,12 @@ from asn1crypto.csr import CertificationRequest
from pinecrypt.server import const, errors, authority
from pinecrypt.server.decorators import csrf_protection, MyEncoder
from pinecrypt.server.user import DirectoryConnection
from pinecrypt.server.mongolog import LogHandler
from oscrypto import asymmetric
from .utils.firewall import whitelist_subnets, whitelist_content_types, \
login_required, login_optional, authorize_admin, validate_clock_skew
logger = logging.getLogger(__name__)
logger = LogHandler()
"""
openssl genrsa -out test.key 1024

View File

@ -2,8 +2,9 @@ import falcon
import logging
from pinecrypt.server import authority, const, errors
from .utils.firewall import whitelist_subnets
from pinecrypt.server.mongolog import LogHandler
logger = logging.getLogger(__name__)
logger = LogHandler()
class RevocationListResource(object):
@whitelist_subnets(const.CRL_SUBNETS)

View File

@ -3,9 +3,10 @@ import os
from pinecrypt.server import authority, const
from jinja2 import Environment, FileSystemLoader
from .utils.firewall import whitelist_subject
from pinecrypt.server.mongolog import LogHandler
logger = logging.getLogger(__name__)
env = Environment(loader=FileSystemLoader(const.SCRIPT_DIR), trim_blocks=True)
logger = LogHandler()
class ScriptResource(object):
@whitelist_subject

View File

@ -4,9 +4,9 @@ from pinecrypt.server import authority, const, config
from pinecrypt.server.decorators import serialize, csrf_protection
from pinecrypt.server.user import User
from .utils.firewall import login_required, authorize_admin, register_session
from pinecrypt.server.mongolog import LogHandler
logger = logging.getLogger(__name__)
logger = LogHandler()
class CertificateAuthorityResource(object):
def on_get(self, req, resp):

View File

@ -1,4 +1,3 @@
import falcon
import logging
import json
@ -6,8 +5,9 @@ import hashlib
from pinecrypt.server import authority, errors, db
from pinecrypt.server.decorators import csrf_protection
from .utils.firewall import login_required, authorize_admin
from pinecrypt.server.mongolog import LogHandler
logger = logging.getLogger(__name__)
logger = LogHandler()
class SignedCertificateDetailResource(object):
def on_get_cn(self, req, resp, cn):

View File

@ -7,8 +7,9 @@ from pinecrypt.server import const, errors, authority
from pinecrypt.server.decorators import serialize
from pinecrypt.server.user import User
from .utils.firewall import login_required, authorize_admin
from pinecrypt.server.mongolog import LogHandler
logger = logging.getLogger(__name__)
logger = LogHandler()
class TokenResource(object):
def __init__(self, manager):

View File

@ -15,6 +15,7 @@ from datetime import datetime, timedelta
from pinecrypt.server.user import User
from pinecrypt.server import const, errors, db
from prometheus_client import Counter, Histogram
from pinecrypt.server.mongolog import LogHandler
clock_skew = Histogram(
"pinecrypt_gateway_clock_skew",
@ -26,8 +27,7 @@ whitelist_blocked_requests = Counter(
"Requests blocked by whitelists.",
["method", "path"])
logger = logging.getLogger(__name__)
logger = LogHandler()
def whitelist_subnets(subnets):
"""

View File

@ -10,12 +10,14 @@ from asn1crypto.csr import CertificationRequest
from certbuilder import CertificateBuilder
from pinecrypt.server import mailer, const, errors, config, db
from pinecrypt.server.common import cn_to_dn, generate_serial, cert_to_dn
from pinecrypt.server.mongolog import LogHandler
from crlbuilder import CertificateListBuilder, pem_armor_crl
from csrbuilder import CSRBuilder, pem_armor_csr
from datetime import datetime, timedelta
from bson.objectid import ObjectId
logger = logging.getLogger(__name__)
#logger = logging.getLogger(__name__)
logger = LogHandler()
# Cache CA certificate
with open(const.AUTHORITY_CERTIFICATE_PATH, "rb") as fh:

View File

@ -26,12 +26,14 @@ from math import log, ceil
from pinecrypt.server import const, mongolog, mailer, db
from pinecrypt.server.middleware import NormalizeMiddleware, PrometheusEndpoint
from pinecrypt.server.common import cn_to_dn, generate_serial
from pinecrypt.server.mongolog import LogHandler
#from pinecrypt.server.logger import CertidudeLogger
from time import sleep
from wsgiref.simple_server import make_server
logger = logging.getLogger(__name__)
mongolog.register()
#logger = logging.getLogger(__name__)
#logger = CertidudeLogger()
logger = LogHandler()
def graceful_exit(signal_number, stack_frame):
print("Received signal %d, exiting now" % signal_number)

View File

@ -0,0 +1,28 @@
import time
class LoggerObject(object):
msg = None
args = None
levelname = None
created = None
class CertidudeLogger(object):
def info(self, msg, *args):
self.pre_emit(msg, *args, level="Info")
def warning(self, msg, *args):
self.pre_emit(msg, *args, level="Warning")
def error(self, msg, *args):
self.pre_emit(msg, *args, level="Error")
def debug(self, msg, *args):
self.pre_emit(msg, *args, level="Debug")
def pre_emit(self, msg, *args, level):
record = LoggerObject()
record.msg = msg
record.args = args
record.levelname = level
record.created = time.time()
self.emit(record)
def emit(self,record):
console.log(record)

View File

@ -1,27 +1,20 @@
import logging
from datetime import datetime
from pinecrypt.server import db
from pinecrypt.server.logger import CertidudeLogger
class LogHandler(logging.Handler):
class LogHandler(CertidudeLogger):
def emit(self, record):
d= {}
d["created"] = datetime.utcfromtimestamp(record.created)
d["facility"] = record.name
d["level"] = record.levelno
# d["facility"] = record.name
# d["level"] = record.levelno
d["severity"] = record.levelname.lower()
d["message"] = record.msg % record.args
d["module"] = record.module
d["func"] = record.funcName
d["lineno"] = record.lineno
d["exception"] = logging._defaultFormatter.formatException(record.exc_info) if record.exc_info else "",
d["process"] = record.process
d["thread"] = record.thread
d["thread_name"] = record.threadName
# d["module"] = record.module
# d["func"] = record.funcName
# d["lineno"] = record.lineno
# d["exception"] = logging._defaultFormatter.formatException(record.exc_info) if record.exc_info else "",
# d["process"] = record.process
# d["thread"] = record.thread
# d["thread_name"] = record.threadName
db.eventlog.insert(d, w=1)
def register():
for j in logging.Logger.manager.loggerDict.values():
if isinstance(j, logging.Logger):
j.setLevel(logging.DEBUG)
j.addHandler(LogHandler())