|
| 1 | +package vertigo |
| 2 | + |
| 3 | +// Copyright (c) 2019 Micro Focus or one of its affiliates. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +// |
| 17 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 18 | +// of this software and associated documentation files (the "Software"), to deal |
| 19 | +// in the Software without restriction, including without limitation the rights |
| 20 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 21 | +// copies of the Software, and to permit persons to whom the Software is |
| 22 | +// furnished to do so, subject to the following conditions: |
| 23 | +// |
| 24 | +// The above copyright notice and this permission notice shall be included in |
| 25 | +// all copies or substantial portions of the Software. |
| 26 | +// |
| 27 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 28 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 29 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 30 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 31 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 32 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 33 | +// THE SOFTWARE. |
| 34 | + |
| 35 | +import ( |
| 36 | + "testing" |
| 37 | + |
| 38 | + "github.com/vertica/vertica-sql-go/logger" |
| 39 | +) |
| 40 | + |
| 41 | +type LogMessage struct { |
| 42 | + Prefix string |
| 43 | + Name string |
| 44 | + Msg string |
| 45 | +} |
| 46 | +type MyLogger struct { |
| 47 | + LastMessage *LogMessage |
| 48 | +} |
| 49 | +func (l *MyLogger) Write(prefix string, name string, msg string) { |
| 50 | + l.LastMessage = &LogMessage{Prefix: prefix, Name: name, Msg: msg} |
| 51 | +} |
| 52 | +func (l MyLogger) Close() {} |
| 53 | + |
| 54 | +func TestExternalLogging(t *testing.T) { |
| 55 | + ml := &MyLogger{} |
| 56 | + |
| 57 | + logger.SetLogger(ml) |
| 58 | + logger.SetLogLevel(logger.TRACE) |
| 59 | + |
| 60 | + log := logger.New("TestExternalLogging") |
| 61 | + |
| 62 | + log.Debug("Basic logging message here") |
| 63 | + assertEqual(t, ml.LastMessage.Prefix, "DEBUG") |
| 64 | + assertEqual(t, ml.LastMessage.Name, "TestExternalLogging") |
| 65 | + assertEqual(t, ml.LastMessage.Msg, "Basic logging message here") |
| 66 | + |
| 67 | + log.Info("Basic logging message here with number %v", 1234) |
| 68 | + assertEqual(t, ml.LastMessage.Prefix, "INFO") |
| 69 | + assertEqual(t, ml.LastMessage.Name, "TestExternalLogging") |
| 70 | + assertEqual(t, ml.LastMessage.Msg, "Basic logging message here with number 1234") |
| 71 | + |
| 72 | + log.Warn("With a string and a number: %s, %d", "a string", 5678) |
| 73 | + assertEqual(t, ml.LastMessage.Prefix, "WARN") |
| 74 | + assertEqual(t, ml.LastMessage.Name, "TestExternalLogging") |
| 75 | + assertEqual(t, ml.LastMessage.Msg, "With a string and a number: a string, 5678") |
| 76 | + |
| 77 | + log.LineTrace() |
| 78 | + assertEqual(t, ml.LastMessage.Prefix, "TRACE") |
| 79 | + assertEqual(t, ml.LastMessage.Name, "TestExternalLogging") |
| 80 | +} |
0 commit comments