You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@zookeeper.apache.org by anmolnar <gi...@git.apache.org> on 2018/04/23 15:29:36 UTC

[GitHub] zookeeper pull request #338: ZOOKEEPER-1260:Audit logging in ZooKeeper serve...

Github user anmolnar commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/338#discussion_r183432327
  
    --- Diff: src/java/main/org/apache/zookeeper/audit/ZKAuditLogger.java ---
    @@ -0,0 +1,117 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you 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
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * 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.
    + */
    +package org.apache.zookeeper.audit;
    +
    +import org.apache.zookeeper.server.ServerCnxnFactory;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +public class ZKAuditLogger {
    +    public static final String SYSPROP_AUDIT_ENABLED = "zookeeper.audit.enabled";
    +    private static final Logger LOG = LoggerFactory.getLogger(ZKAuditLogger.class);
    +    // By default audit logging is disabled
    +    public static final boolean isAuditEnabled = Boolean.getBoolean(SYSPROP_AUDIT_ENABLED);
    +
    +    /**
    +     *
    +     * Prints audit log based on log level specified
    +     *
    +     */
    +    public static enum LogLevel {
    +        ERROR {
    +            @Override
    +            public void printLog(String logMsg) {
    +                LOG.error(logMsg);
    +            }
    +        },
    +        INFO {
    +            @Override
    +            public void printLog(String logMsg) {
    +                LOG.info(logMsg);
    +            }
    +        };
    +        public abstract void printLog(String logMsg);
    +    }
    +
    +    public static enum Keys {
    +        USER, OPERATION, RESULT, IP, ACL, ZNODE, SESSION;
    +    }
    +
    +    public static void logInvoked(String user, String operation) {
    +        log(LogLevel.INFO, user, operation, AuditConstants.INVOKED);
    +    }
    +
    +    public static void logSuccess(String user, String operation) {
    +        log(LogLevel.INFO, user, operation, AuditConstants.SUCCESS);
    +    }
    +
    +    public static void logFailure(String user, String operation) {
    +        log(LogLevel.ERROR, user, operation, AuditConstants.FAILURE);
    +    }
    +
    +    private static void log(LogLevel level, String user, String operation, String logType) {
    +        level.printLog(createLog(user, operation, null, null, null, null, logType));
    +    }
    +
    +    public static void logSuccess(String user, String operation, String znode, String acl, String session, String ip) {
    +        LogLevel.INFO.printLog(createLog(user, operation, znode, acl, session, ip, AuditConstants.SUCCESS));
    +    }
    +
    +    public static void logFailure(String user, String operation, String znode, String acl, String session, String ip) {
    +        LogLevel.ERROR.printLog(createLog(user, operation, znode, acl, session, ip, AuditConstants.FAILURE));
    +    }
    +
    +    /**
    +     * A helper api for creating an audit log string.
    +     */
    +    public static String createLog(String user, String operation, String znode, String acl, String session, String ip,
    +            String status) {
    +        ZKAuditLogFormatter fmt = new ZKAuditLogFormatter();
    --- End diff --
    
    If I got it right, this is the only place where you use `ZKAuditLogFormatter`. Why don't you just use `String.format()` instead of a custom formatter?


---