github.com/ledgerwatch/erigon-lib@v1.0.0/downloader/downloadercfg/logger.go (about)

     1  /*
     2     Copyright 2021 Erigon contributors
     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         http://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 downloadercfg
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	lg "github.com/anacrolix/log"
    24  	"github.com/ledgerwatch/log/v3"
    25  )
    26  
    27  func init() {
    28  	lg.Default.Handlers = []lg.Handler{adapterHandler{}}
    29  }
    30  
    31  func Int2LogLevel(level int) (lvl lg.Level, dbg bool, err error) {
    32  	switch level {
    33  	case 0:
    34  		lvl = lg.Critical
    35  	case 1:
    36  		lvl = lg.Error
    37  	case 2:
    38  		lvl = lg.Warning
    39  	case 3:
    40  		lvl = lg.Info
    41  	case 4:
    42  		lvl = lg.Debug
    43  	case 5:
    44  		lvl = lg.Debug
    45  		dbg = true
    46  	default:
    47  		return lvl, dbg, fmt.Errorf("invalid level set, expected a number between 0-5 but got: %d", level)
    48  	}
    49  	return lvl, dbg, nil
    50  }
    51  
    52  type noopHandler struct{}
    53  
    54  func (b noopHandler) Handle(r lg.Record) {
    55  }
    56  
    57  type adapterHandler struct{}
    58  
    59  func (b adapterHandler) Handle(r lg.Record) {
    60  	lvl := r.Level
    61  
    62  	switch lvl {
    63  	case lg.Debug:
    64  		log.Info("[downloader] " + r.String())
    65  	case lg.Info:
    66  		str := r.String()
    67  		if strings.Contains(str, "EOF") ||
    68  			strings.Contains(str, "spurious timer") ||
    69  			strings.Contains(str, "banning ip <nil>") { // suppress useless errors
    70  			break
    71  		}
    72  
    73  		log.Info(str)
    74  	case lg.Warning:
    75  		str := r.String()
    76  		if strings.Contains(str, "could not find offer for id") { // suppress useless errors
    77  			break
    78  		}
    79  		if strings.Contains(str, "webrtc conn for unloaded torrent") { // suppress useless errors
    80  			break
    81  		}
    82  		if strings.Contains(str, "TrackerClient closed") { // suppress useless errors
    83  			break
    84  		}
    85  		if strings.Contains(str, "banned ip") { // suppress useless errors
    86  			break
    87  		}
    88  		if strings.Contains(str, "being sole dirtier of piece") { // suppress useless errors
    89  			break
    90  		}
    91  		if strings.Contains(str, "requested chunk too long") { // suppress useless errors
    92  			break
    93  		}
    94  		if strings.Contains(str, "reservation cancelled") { // suppress useless errors
    95  			break
    96  		}
    97  		if strings.Contains(str, "received invalid reject") { // suppress useless errors
    98  			break
    99  		}
   100  
   101  		log.Warn(str)
   102  	case lg.Error:
   103  		str := r.String()
   104  		if strings.Contains(str, "EOF") { // suppress useless errors
   105  			break
   106  		}
   107  
   108  		log.Error(str)
   109  	case lg.Critical:
   110  		str := r.String()
   111  		if strings.Contains(str, "EOF") { // suppress useless errors
   112  			break
   113  		}
   114  		if strings.Contains(str, "don't want conns") { // suppress useless errors
   115  			break
   116  		}
   117  		if strings.Contains(str, "torrent closed") { // suppress useless errors
   118  			break
   119  		}
   120  
   121  		log.Error(str)
   122  	default:
   123  		log.Info("[downloader] "+r.String(), "torrent_log_type", "unknown", "or", lvl.LogString())
   124  	}
   125  }