|
| 1 | +/* |
| 2 | + * Copyright 2023-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.template.st; |
| 18 | + |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.stringtemplate.v4.STErrorListener; |
| 21 | +import org.stringtemplate.v4.misc.ErrorType; |
| 22 | +import org.stringtemplate.v4.misc.STMessage; |
| 23 | + |
| 24 | +/** |
| 25 | + * An {@link STErrorListener} that delegates to {@link Logger slf4j}. |
| 26 | + * |
| 27 | + * @author Sun Yuhan |
| 28 | + */ |
| 29 | +public class Slf4jStErrorListener implements STErrorListener { |
| 30 | + |
| 31 | + private final Logger logger; |
| 32 | + |
| 33 | + /* package */ Slf4jStErrorListener(Logger logger) { |
| 34 | + this.logger = logger; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void compileTimeError(STMessage msg) { |
| 39 | + logger.error(msg.toString()); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void runTimeError(STMessage msg) { |
| 44 | + if (msg.error != ErrorType.NO_SUCH_PROPERTY) { // ignore these |
| 45 | + logger.error(msg.toString()); |
| 46 | + } |
| 47 | + else { |
| 48 | + logger.warn(msg.toString()); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void IOError(STMessage msg) { |
| 54 | + logger.error(msg.toString()); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void internalError(STMessage msg) { |
| 59 | + logger.error(msg.toString()); |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments