View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.fit.core.reference;
20  
21  import java.util.Collections;
22  import java.util.Set;
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.core.LoggerContext;
26  import org.apache.logging.log4j.core.appender.FileAppender;
27  import org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender;
28  import org.apache.logging.log4j.core.appender.rewrite.RewritePolicy;
29  import org.apache.logging.log4j.core.layout.PatternLayout;
30  import org.apache.syncope.common.lib.types.AuditElements;
31  import org.apache.syncope.common.lib.types.AuditLoggerName;
32  import org.apache.syncope.core.logic.ResourceLogic;
33  import org.apache.syncope.core.logic.audit.DefaultRewriteAuditAppender;
34  
35  public class TestFileRewriteAuditAppender extends DefaultRewriteAuditAppender {
36  
37      public TestFileRewriteAuditAppender(final String domain) {
38          super(domain);
39  
40          LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
41  
42          // get log file path from existing file appender
43          RollingRandomAccessFileAppender main =
44                  (RollingRandomAccessFileAppender) ctx.getConfiguration().getAppender("main");
45          String pathPrefix = StringUtils.replace(main.getFileName(), "core.log", StringUtils.EMPTY);
46  
47          targetAppender = FileAppender.newBuilder().
48                  setName(getTargetAppenderName()).
49                  withAppend(true).
50                  withFileName(pathPrefix + getTargetAppenderName() + ".log").
51                  setLayout(PatternLayout.newBuilder().
52                          withPattern("%d{HH:mm:ss.SSS} %-5level %logger - %msg%n").
53                          build()).
54                  build();
55      }
56  
57      @Override
58      public Set<AuditLoggerName> getEvents() {
59          return Collections.singleton(new AuditLoggerName(
60                  AuditElements.EventCategoryType.LOGIC,
61                  ResourceLogic.class.getSimpleName(),
62                  null,
63                  "update",
64                  AuditElements.Result.SUCCESS));
65      }
66  
67      @Override
68      public String getTargetAppenderName() {
69          return "audit_for_" + domain + "_file";
70      }
71  
72      @Override
73      protected RewritePolicy getRewritePolicy() {
74          return TestRewritePolicy.createPolicy();
75      }
76  }