...

Source file src/gitlab.com/tymonx/go-logger/logger/utils.go

Documentation: gitlab.com/tymonx/go-logger/logger

     1  // Copyright 2020 Tymoteusz Blazejczyk
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package logger
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  	"os"
    21  )
    22  
    23  // Named is used as named string placeholders for logger functions.
    24  type Named map[string]interface{}
    25  
    26  // getHostname returns local hostname.
    27  func getHostname() (string, error) {
    28  	hostname, err := os.Hostname()
    29  
    30  	if err != nil {
    31  		return "localhost", NewRuntimeError("cannot get hostname", err)
    32  	}
    33  
    34  	return hostname, nil
    35  }
    36  
    37  // getAddress returns local IP address.
    38  func getAddress() (string, error) {
    39  	connection, err := net.Dial("udp", "8.8.8.8:80")
    40  
    41  	if err != nil {
    42  		return "127.0.0.1", NewRuntimeError("cannot connect to primary Google DNS", err)
    43  	}
    44  
    45  	defer func() {
    46  		err := connection.Close()
    47  
    48  		if err != nil {
    49  			printError(NewRuntimeError("cannot close UDP connection", err))
    50  		}
    51  	}()
    52  
    53  	return connection.LocalAddr().(*net.UDPAddr).IP.String(), nil
    54  }
    55  
    56  // printError prints error to error output.
    57  func printError(err error) {
    58  	fmt.Fprintln(os.Stderr, "Logger error:", err)
    59  }
    60  

View as plain text