update docs
[yosql.git] / yosql-codegen / src / main / java / wtf / metio / yosql / codegen / logging / SystemLoggingGenerator.java
blob448f1c06378bd3673fa7c61a1aca16914a595d67
1 /*
2 * This file is part of yosql. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at https://creativecommons.org/publicdomain/zero/1.0/. No part of yosql,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
5 * in the LICENSE file.
6 */
7 package wtf.metio.yosql.codegen.logging;
9 import com.squareup.javapoet.CodeBlock;
10 import com.squareup.javapoet.FieldSpec;
11 import com.squareup.javapoet.TypeName;
12 import wtf.metio.yosql.codegen.blocks.Fields;
13 import wtf.metio.yosql.models.configuration.LoggingApis;
14 import wtf.metio.yosql.models.immutables.NamesConfiguration;
16 import java.util.Optional;
18 /**
19 * Logging generator that uses {@link System.Logger}.
21 public final class SystemLoggingGenerator implements LoggingGenerator {
23 private final NamesConfiguration names;
24 private final Fields fields;
26 public SystemLoggingGenerator(final NamesConfiguration names, final Fields fields) {
27 this.names = names;
28 this.fields = fields;
31 @Override
32 public Optional<FieldSpec> logger(final TypeName repoClass) {
33 return Optional.of(fields.prepareConstant(System.Logger.class, names.logger())
34 .initializer("$T.getLogger($S)", System.class, repoClass.toString())
35 .build());
38 @Override
39 public boolean supports(final LoggingApis api) {
40 return LoggingApis.SYSTEM.equals(api);
43 @Override
44 public CodeBlock queryPicked(final String fieldName) {
45 return CodeBlock.builder()
46 .addStatement("$N.log($T.DEBUG, $T.format($S, $S))", names.logger(), System.Logger.Level.class,
47 String.class, "Picked query [%s]", fieldName)
48 .build();
51 @Override
52 public CodeBlock indexPicked(final String fieldName) {
53 return CodeBlock.builder()
54 .addStatement("$N.log($T.DEBUG, $T.format($S, $S))", names.logger(), System.Logger.Level.class,
55 String.class, "Picked index [%s]", fieldName)
56 .build();
59 @Override
60 public CodeBlock vendorQueryPicked(final String fieldName) {
61 return CodeBlock.builder()
62 .addStatement("$N.log($T.DEBUG, $T.format($S, $S))", names.logger(), System.Logger.Level.class,
63 String.class, "Picked query [%s]", fieldName)
64 .build();
67 @Override
68 public CodeBlock vendorIndexPicked(final String fieldName) {
69 return CodeBlock.builder()
70 .addStatement("$N.log($T.DEBUG, $T.format($S, $S))", names.logger(), System.Logger.Level.class,
71 String.class, "Picked index [%s]", fieldName)
72 .build();
75 @Override
76 public CodeBlock vendorDetected() {
77 return CodeBlock.builder()
78 .addStatement("$N.log($T.INFO, $T.format($S, $S))", names.logger(), System.Logger.Level.class,
79 String.class, "Detected database vendor [%s]", names.databaseProductName())
80 .build();
83 @Override
84 public CodeBlock executingQuery() {
85 return CodeBlock.builder()
86 .addStatement("$N.log($T.INFO, $T.format($S, $N))", names.logger(), System.Logger.Level.class,
87 String.class, "Executing query [%s]", names.executedQuery())
88 .build();
91 @Override
92 public CodeBlock shouldLog() {
93 return CodeBlock.builder().add("$N.isLoggable($T.INFO)", names.logger(), System.Logger.Level.class).build();
96 @Override
97 public boolean isEnabled() {
98 return true;
101 @Override
102 public CodeBlock entering(final String repository, final String method) {
103 return CodeBlock.builder()
104 .addStatement("$N.log($T.DEBUG, $T.format($S, $S, $S))", names.logger(), System.Logger.Level.class,
105 String.class, "Entering [%s#%s]", repository, method)
106 .build();