Hybris:如何在同一扩展名中创建单独的 log4j 日志文件

问题描述

我需要将一些审核日志记录到与标准控制台日志不同的文件中。我尝试使用添加自定义扩展的属性创建自定义记录器

    Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py",line 1276,in _execute_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py",line 608,in do_execute
    cursor.execute(statement,parameters)
psycopg2.errors.UndefinedColumn: column dashboards.uuid does not exist
LINE 1: SELECT dashboards.uuid AS dashboards_uuid,dashboards.create...
               ^
HINT:  Perhaps you meant to reference the column "dashboards.id".


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/flask/app.py",line 2447,in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.8/site-packages/flask/app.py",line 1952,in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.8/site-packages/flask/app.py",line 1821,in handle_user_exception
    reraise(exc_type,exc_value,tb)
  File "/usr/local/lib/python3.8/site-packages/flask/_compat.py",line 39,in reraise
    raise value
  File "/usr/local/lib/python3.8/site-packages/flask/app.py",line 1950,in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.8/site-packages/flask/app.py",line 1936,in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib/python3.8/site-packages/flask_appbuilder/security/decorators.py",line 109,in wraps
    return f(self,*args,**kwargs)
  File "/usr/local/lib/python3.8/site-packages/superset/utils/log.py",line 162,in wrapper
    value = f(*args,add_extra_log_payload=log,**kwargs)
  File "/usr/local/lib/python3.8/site-packages/superset/views/core.py",line 1803,in dashboard
    dash = qry.one_or_none()
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/query.py",line 3459,in one_or_none
    ret = list(self)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/query.py",line 3535,in __iter__
    return self._execute_and_instances(context)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/query.py",line 3560,in _execute_and_instances
    result = conn.execute(querycontext.statement,self._params)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py",line 1011,in execute
    return meth(self,multiparams,params)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/sql/elements.py",line 298,in _execute_on_connection
    return connection._execute_clauseelement(self,params)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py",line 1124,in _execute_clauseelement
    ret = self._execute_context(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py",line 1316,in _execute_context
    self._handle_dbapi_exception(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py",line 1510,in _handle_dbapi_exception
    util.raise_(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py",line 182,in raise_
    raise exception
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py",parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedColumn) column dashboards.uuid does not exist
LINE 1: SELECT dashboards.uuid AS dashboards_uuid,dashboards.create...
               ^
HINT:  Perhaps you meant to reference the column "dashboards.id".

[sql: SELECT dashboards.uuid AS dashboards_uuid,dashboards.created_on AS dashboards_created_on,dashboards.changed_on AS dashboards_changed_on,dashboards.id AS dashboards_id,dashboards.dashboard_title AS dashboards_dashboard_title,dashboards.position_json AS dashboards_position_json,dashboards.description AS dashboards_description,dashboards.css AS dashboards_css,dashboards.json_Metadata AS dashboards_json_Metadata,dashboards.slug AS dashboards_slug,dashboards.published AS dashboards_published,dashboards.created_by_fk AS dashboards_created_by_fk,dashboards.changed_by_fk AS dashboards_changed_by_fk 
FROM dashboards 
WHERE dashboards.id = %(id_1)s]
[parameters: {'id_1': 1}]
(Background on this error at: http://sqlalche.me/e/13/f405)

然后我将记录器初始化为:

log4j.appender.auditLog=org.apache.log4j.FileAppender
log4j.appender.auditLog.File=/somelogsfile.log
log4j.appender.auditLog.layout=org.apache.log4j.PatternLayout
log4j.appender.auditLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n

log4j.category.auditLogger=INFO,auditLog
log4j.additivity.auditLogger=false

日志输出到标准控制台日志,但不输出到指定的文件。我错过了什么?

解决方法

根据更新的问题,我做了一些额外的调查,并在我自己的系统上进行了测试。

很可能,您的 hybris 版本正在使用 log4j2 这意味着配置应该是这样的:

log4j2.appender.auditLog.type=File
log4j2.appender.auditLog.name=auditLog
log4j2.appender.auditLog.fileName=${HYBRIS_LOG_DIR}/audit.log
log4j2.appender.auditLog.layout.type=PatternLayout
log4j2.appender.auditLog.layout.pattern=%d [%24F:%t:%L] - %m%n

log4j2.logger.auditLogger.name=auditLogger
log4j2.logger.auditLogger.level=info
log4j2.logger.auditLogger.appenderRefs=auditLogger
log4j2.logger.auditLogger.appenderRef.auditLogger.ref=auditLog
log4j2.logger.auditLogger.additivity=false

我已在本地系统上对此进行了测试,并使用 Logger LOG= Logger.getLogger("auditLogger") 将日志语句打印到新文件中