github.com/Psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/logger.go (about)

     1  /*
     2   * Copyright (c) 2017, Psiphon Inc.
     3   * All rights reserved.
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU 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 General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package common
    21  
    22  // Logger exposes a logging interface that's compatible with
    23  // psiphon/server.TraceLogger. This interface allows packages
    24  // to implement logging that will integrate with psiphon/server
    25  // without importing that package. Other implementations of
    26  // Logger may also be provided.
    27  type Logger interface {
    28  	WithTrace() LogTrace
    29  	WithTraceFields(fields LogFields) LogTrace
    30  	LogMetric(metric string, fields LogFields)
    31  }
    32  
    33  // LogTrace is interface-compatible with the return values from
    34  // psiphon/server.TraceLogger.WitTrace/WithTraceFields.
    35  type LogTrace interface {
    36  	Debug(args ...interface{})
    37  	Info(args ...interface{})
    38  	Warning(args ...interface{})
    39  	Error(args ...interface{})
    40  }
    41  
    42  // LogFields is type-compatible with psiphon/server.LogFields
    43  // and logrus.LogFields.
    44  type LogFields map[string]interface{}
    45  
    46  // Add copies log fields from b to a, skipping fields which already exist,
    47  // regardless of value, in a.
    48  func (a LogFields) Add(b LogFields) {
    49  	for name, value := range b {
    50  		_, ok := a[name]
    51  		if !ok {
    52  			a[name] = value
    53  		}
    54  	}
    55  }
    56  
    57  // MetricsSource is an object that provides metrics to be logged.
    58  type MetricsSource interface {
    59  
    60  	// GetMetrics returns a LogFields populated with metrics from the
    61  	// MetricsSource.
    62  	GetMetrics() LogFields
    63  }
    64  
    65  // NoticeMetricsSource is an object that provides metrics to be logged
    66  // only in notices, for inclusion in diagnostics.
    67  type NoticeMetricsSource interface {
    68  
    69  	// GetNoticeMetrics returns a LogFields populated with metrics from
    70  	// the NoticeMetricsSource.
    71  	GetNoticeMetrics() LogFields
    72  }