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.io.File;
22  import java.util.HashSet;
23  import java.util.Set;
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.core.LoggerContext;
27  import org.apache.logging.log4j.core.appender.FileAppender;
28  import org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender;
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.ConnectorLogic;
33  import org.apache.syncope.core.logic.ResourceLogic;
34  import org.apache.syncope.core.logic.audit.DefaultAuditAppender;
35  
36  public class TestFileAuditAppender extends DefaultAuditAppender {
37  
38      public TestFileAuditAppender(final String domain) {
39          super(domain);
40  
41          LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
42          // get log file path from existing file appender
43          RollingRandomAccessFileAppender main =
44                  (RollingRandomAccessFileAppender) ctx.getConfiguration().getAppender("main");
45  
46          String pathPrefix = main == null
47                  ? System.getProperty("user.dir") + StringUtils.replace("/target/log", "/", File.separator)
48                  + File.separator
49                  : StringUtils.replace(main.getFileName(), "core.log", StringUtils.EMPTY);
50  
51          targetAppender = FileAppender.newBuilder().
52                  setName(getTargetAppenderName()).
53                  withAppend(true).
54                  withFileName(pathPrefix + getTargetAppenderName() + ".log").
55                  setLayout(PatternLayout.newBuilder().
56                          withPattern("%d{HH:mm:ss.SSS} %-5level %logger - %msg%n").
57                          build()).
58                  build();
59      }
60  
61      @Override
62      public Set<AuditLoggerName> getEvents() {
63          Set<AuditLoggerName> events = new HashSet<>();
64          events.add(new AuditLoggerName(
65                  AuditElements.EventCategoryType.LOGIC,
66                  ResourceLogic.class.getSimpleName(),
67                  null,
68                  "create",
69                  AuditElements.Result.SUCCESS));
70          events.add(new AuditLoggerName(
71                  AuditElements.EventCategoryType.LOGIC,
72                  ConnectorLogic.class.getSimpleName(),
73                  null,
74                  "update",
75                  AuditElements.Result.SUCCESS));
76          return events;
77      }
78  
79      @Override
80      public String getTargetAppenderName() {
81          return "audit_for_" + domain + "_norewrite_file";
82      }
83  }