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
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
;
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
) {
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())
39 public boolean supports(final LoggingApis api
) {
40 return LoggingApis
.SYSTEM
.equals(api
);
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
)
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
)
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
)
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
)
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())
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())
92 public CodeBlock
shouldLog() {
93 return CodeBlock
.builder().add("$N.isLoggable($T.INFO)", names
.logger(), System
.Logger
.Level
.class).build();
97 public boolean isEnabled() {
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
)