log.go (2907B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package testrig 19 20 import ( 21 "github.com/superseriousbusiness/gotosocial/internal/config" 22 "github.com/superseriousbusiness/gotosocial/internal/log" 23 "gopkg.in/mcuadros/go-syslog.v2" 24 "gopkg.in/mcuadros/go-syslog.v2/format" 25 ) 26 27 // InitTestLog sets the global logger to trace level for logging 28 func InitTestLog() { 29 // Set the global log level from configuration 30 if err := log.ParseLevel(config.GetLogLevel()); err != nil { 31 log.Panicf(nil, "error parsing log level: %v", err) 32 } 33 34 if config.GetSyslogEnabled() { 35 // Enable logging to syslog 36 if err := log.EnableSyslog( 37 config.GetSyslogProtocol(), 38 config.GetSyslogAddress(), 39 ); err != nil { 40 log.Panicf(nil, "error enabling syslogging: %v", err) 41 } 42 } 43 } 44 45 // InitTestSyslog returns a test syslog running on port 42069 and a channel for reading 46 // messages sent to the server, or an error if something goes wrong. 47 // 48 // Callers of this function should call Kill() on the server when they're finished with it! 49 func InitTestSyslog() (*syslog.Server, chan format.LogParts, error) { 50 channel := make(syslog.LogPartsChannel) 51 handler := syslog.NewChannelHandler(channel) 52 53 server := syslog.NewServer() 54 server.SetFormat(syslog.Automatic) 55 server.SetHandler(handler) 56 57 if err := server.ListenUDP("127.0.0.1:42069"); err != nil { 58 return nil, nil, err 59 } 60 61 if err := server.Boot(); err != nil { 62 return nil, nil, err 63 } 64 65 return server, channel, nil 66 } 67 68 // InitTestSyslog returns a test syslog running on a unix socket, and a channel for reading 69 // messages sent to the server, or an error if something goes wrong. 70 // 71 // Callers of this function should call Kill() on the server when they're finished with it! 72 func InitTestSyslogUnixgram(address string) (*syslog.Server, chan format.LogParts, error) { 73 channel := make(syslog.LogPartsChannel) 74 handler := syslog.NewChannelHandler(channel) 75 76 server := syslog.NewServer() 77 server.SetFormat(syslog.Automatic) 78 server.SetHandler(handler) 79 80 if err := server.ListenUnixgram(address); err != nil { 81 return nil, nil, err 82 } 83 84 if err := server.Boot(); err != nil { 85 return nil, nil, err 86 } 87 88 return server, channel, nil 89 }