1
#include "source/extensions/access_loggers/file/config.h"
2

            
3
#include <memory>
4

            
5
#include "envoy/extensions/access_loggers/file/v3/file.pb.h"
6
#include "envoy/extensions/access_loggers/file/v3/file.pb.validate.h"
7
#include "envoy/registry/registry.h"
8
#include "envoy/server/filter_config.h"
9

            
10
#include "source/common/common/logger.h"
11
#include "source/common/config/utility.h"
12
#include "source/common/formatter/substitution_format_string.h"
13
#include "source/common/formatter/substitution_formatter.h"
14
#include "source/common/protobuf/protobuf.h"
15
#include "source/extensions/access_loggers/common/file_access_log_impl.h"
16

            
17
namespace Envoy {
18
namespace Extensions {
19
namespace AccessLoggers {
20
namespace File {
21

            
22
AccessLog::InstanceSharedPtr FileAccessLogFactory::createAccessLogInstance(
23
    const Protobuf::Message& config, AccessLog::FilterPtr&& filter,
24
    Server::Configuration::GenericFactoryContext& context,
25
20389
    std::vector<Formatter::CommandParserPtr>&& command_parsers) {
26
20389
  const auto& fal_config = MessageUtil::downcastAndValidate<
27
20389
      const envoy::extensions::access_loggers::file::v3::FileAccessLog&>(
28
20389
      config, context.messageValidationVisitor());
29
20389
  Formatter::FormatterPtr formatter;
30

            
31
20389
  switch (fal_config.access_log_format_case()) {
32
5
  case envoy::extensions::access_loggers::file::v3::FileAccessLog::AccessLogFormatCase::kFormat:
33
5
    if (fal_config.format().empty()) {
34
4
      formatter = THROW_OR_RETURN_VALUE(
35
4
          Formatter::HttpSubstitutionFormatUtils::defaultSubstitutionFormatter(),
36
4
          Formatter::FormatterPtr);
37
4
    } else {
38
1
      envoy::config::core::v3::SubstitutionFormatString sff_config;
39
1
      sff_config.mutable_text_format_source()->set_inline_string(fal_config.format());
40
1
      formatter = THROW_OR_RETURN_VALUE(Formatter::SubstitutionFormatStringUtils::fromProtoConfig(
41
1
                                            sff_config, context, std::move(command_parsers)),
42
1
                                        Formatter::FormatterPtr);
43
1
    }
44
5
    break;
45
1
  case envoy::extensions::access_loggers::file::v3::FileAccessLog::AccessLogFormatCase::kJsonFormat:
46
1
    formatter = Formatter::SubstitutionFormatStringUtils::createJsonFormatter(
47
1
        fal_config.json_format(), false, command_parsers);
48
1
    break;
49
1
  case envoy::extensions::access_loggers::file::v3::FileAccessLog::AccessLogFormatCase::
50
1
      kTypedJsonFormat: {
51
1
    envoy::config::core::v3::SubstitutionFormatString sff_config;
52
1
    *sff_config.mutable_json_format() = fal_config.typed_json_format();
53
1
    formatter = THROW_OR_RETURN_VALUE(Formatter::SubstitutionFormatStringUtils::fromProtoConfig(
54
1
                                          sff_config, context, std::move(command_parsers)),
55
1
                                      Formatter::FormatterPtr);
56
1
    break;
57
  }
58
1761
  case envoy::extensions::access_loggers::file::v3::FileAccessLog::AccessLogFormatCase::kLogFormat:
59
1761
    formatter =
60
1761
        THROW_OR_RETURN_VALUE(Formatter::SubstitutionFormatStringUtils::fromProtoConfig(
61
1761
                                  fal_config.log_format(), context, std::move(command_parsers)),
62
1761
                              Formatter::FormatterPtr);
63
1761
    break;
64
18620
  case envoy::extensions::access_loggers::file::v3::FileAccessLog::AccessLogFormatCase::
65
18620
      ACCESS_LOG_FORMAT_NOT_SET:
66
18620
    formatter = THROW_OR_RETURN_VALUE(
67
18620
        Formatter::HttpSubstitutionFormatUtils::defaultSubstitutionFormatter(),
68
18620
        Formatter::FormatterPtr);
69
18620
    break;
70
20389
  }
71

            
72
20386
  Filesystem::FilePathAndType file_info{Filesystem::DestinationType::File, fal_config.path()};
73
20386
  return std::make_shared<FileAccessLog>(file_info, std::move(filter), std::move(formatter),
74
20386
                                         context.serverFactoryContext().accessLogManager());
75
20389
}
76

            
77
21393
ProtobufTypes::MessagePtr FileAccessLogFactory::createEmptyConfigProto() {
78
21393
  return ProtobufTypes::MessagePtr{
79
21393
      new envoy::extensions::access_loggers::file::v3::FileAccessLog()};
80
21393
}
81

            
82
12147
std::string FileAccessLogFactory::name() const { return "envoy.access_loggers.file"; }
83

            
84
/**
85
 * Static registration for the file access log. @see RegisterFactory.
86
 */
87
LEGACY_REGISTER_FACTORY(FileAccessLogFactory, AccessLog::AccessLogInstanceFactory,
88
                        "envoy.file_access_log");
89

            
90
} // namespace File
91
} // namespace AccessLoggers
92
} // namespace Extensions
93
} // namespace Envoy