Best Is code snippet using is.callerinfo
log.go
Source:log.go
1/*2Copyright (c) 2015, Northeastern University3 All rights reserved.4 Redistribution and use in source and binary forms, with or without5 modification, are permitted provided that the following conditions are met:6 * Redistributions of source code must retain the above copyright7 notice, this list of conditions and the following disclaimer.8 * Redistributions in binary form must reproduce the above copyright9 notice, this list of conditions and the following disclaimer in the10 documentation and/or other materials provided with the distribution.11 * Neither the name of the Northeastern University nor the12 names of its contributors may be used to endorse or promote products13 derived from this software without specific prior written permission.14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE17 DISCLAIMED. IN NO EVENT SHALL Northeastern University BE LIABLE FOR ANY18 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/25package log26import (27 "flag"28 "io"29 "os"30 "path"31 "runtime"32 "github.com/sirupsen/logrus"33)34var logger = logrus.New()35// GetLogger returns the current logger36func GetLogger() Logger {37 return logger38}39// Fields are fields to add to a log entry40type Fields map[string]interface{}41// WithFields returns a logger that will log the next line42// with the given fields43func WithFields(f Fields) Logger {44 return callerInfo(0).WithFields(logrus.Fields(f))45}46// WithFieldDepth works like WithFields but allows to set the depth47// so the line info is correct48func WithFieldDepth(f Fields, depth int) Logger {49 return callerInfo(depth).WithFields(logrus.Fields(f))50}51// Logger is the interface that the logging package supports52type Logger interface {53 Debugf(format string, args ...interface{})54 Infof(format string, args ...interface{})55 Printf(format string, args ...interface{})56 Warnf(format string, args ...interface{})57 Warningf(format string, args ...interface{})58 Errorf(format string, args ...interface{})59 Fatalf(format string, args ...interface{})60 Panicf(format string, args ...interface{})61 Debug(args ...interface{})62 Info(args ...interface{})63 Print(args ...interface{})64 Warn(args ...interface{})65 Warning(args ...interface{})66 Error(args ...interface{})67 Fatal(args ...interface{})68 Panic(args ...interface{})69 Debugln(args ...interface{})70 Infoln(args ...interface{})71 Println(args ...interface{})72 Warnln(args ...interface{})73 Warningln(args ...interface{})74 Errorln(args ...interface{})75 Fatalln(args ...interface{})76 Panicln(args ...interface{})77}78type logLevel struct{}79func (ll logLevel) String() string {80 return logger.Level.String()81}82func (ll logLevel) Set(l string) error {83 level, err := logrus.ParseLevel(l)84 if err != nil {85 return err86 }87 logger.Level = level88 return nil89}90func handleLogFile(path string) (io.Writer, error) {91 f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)92 if err != nil {93 return nil, err94 }95 return f, nil96}97type logOutput struct {98 out string99}100func (lo logOutput) String() string {101 return lo.out102}103func (lo logOutput) Set(out string) error {104 switch out {105 case "stdout":106 logger.Out = os.Stdout107 case "stderr":108 logger.Out = os.Stderr109 default:110 w, err := handleLogFile(out)111 if err != nil {112 return err113 }114 logger.Out = w115 return nil116 }117 return nil118}119func callerInfo(depth int) *logrus.Entry {120 // From std lib log library121 _, file, line, ok := runtime.Caller(2 + depth)122 if !ok {123 file = "???"124 line = 0125 }126 return logger.WithFields(127 logrus.Fields{128 "file": path.Base(file),129 "line": line,130 },131 )132}133func init() {134 flag.Var(logLevel{}, "loglevel", "Log level")135 flag.Var(logOutput{}, "logoutput", "Where to send log output")136}137func Debugf(format string, args ...interface{}) {138 callerInfo(0).Debugf(format, args...)139}140func Infof(format string, args ...interface{}) {141 callerInfo(0).Infof(format, args...)142}143func Printf(format string, args ...interface{}) {144 callerInfo(0).Printf(format, args...)145}146func Warnf(format string, args ...interface{}) {147 callerInfo(0).Warnf(format, args...)148}149func Warningf(format string, args ...interface{}) {150 callerInfo(0).Warnf(format, args...)151}152func Errorf(format string, args ...interface{}) {153 callerInfo(0).Errorf(format, args...)154}155func Fatalf(format string, args ...interface{}) {156 callerInfo(0).Fatalf(format, args...)157}158func Panicf(format string, args ...interface{}) {159 callerInfo(0).Panicf(format, args...)160}161func Debug(args ...interface{}) {162 callerInfo(0).Debug(args...)163}164func Info(args ...interface{}) {165 callerInfo(0).Info(args...)166}167func Print(args ...interface{}) {168 callerInfo(0).Info(args...)169}170func Warn(args ...interface{}) {171 callerInfo(0).Warn(args...)172}173func Warning(args ...interface{}) {174 callerInfo(0).Warn(args...)175}176func Error(args ...interface{}) {177 callerInfo(0).Error(args...)178}179func Fatal(args ...interface{}) {180 callerInfo(0).Fatal(args...)181}182func Panic(args ...interface{}) {183 callerInfo(0).Panic(args...)184}185func Debugln(args ...interface{}) {186 callerInfo(0).Debugln(args...)187}188func Infoln(args ...interface{}) {189 callerInfo(0).Infoln(args...)190}191func Println(args ...interface{}) {192 callerInfo(0).Println(args...)193}194func Warnln(args ...interface{}) {195 callerInfo(0).Warnln(args...)196}197func Warningln(args ...interface{}) {198 callerInfo(0).Warnln(args...)199}200func Errorln(args ...interface{}) {201 callerInfo(0).Errorln(args...)202}203func Fatalln(args ...interface{}) {204 callerInfo(0).Fatalln(args...)205}206func Panicln(args ...interface{}) {207 callerInfo(0).Panicln(args...)208}...
callerInfo_test.go
Source:callerInfo_test.go
1/*2Copyright SecureKey Technologies Inc. All Rights Reserved.3SPDX-License-Identifier: Apache-2.04*/5package metadata6import (7 "testing"8 "github.com/stretchr/testify/assert"9 "github.com/hyperledger/fabric-sdk-go/pkg/core/logging/api"10)11func TestCallerInfoSetting(t *testing.T) {12 sampleCallerInfoSetting := CallerInfo{}13 samppleModuleName := "sample-module-name"14 //By default caller info should be enabled if not set15 assert.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(samppleModuleName, api.DEBUG), "Callerinfo supposed to be enabled for this level")16 assert.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(samppleModuleName, api.INFO), "Callerinfo supposed to be enabled for this level")17 assert.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(samppleModuleName, api.WARNING), "Callerinfo supposed to be enabled for this level")18 assert.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(samppleModuleName, api.ERROR), "Callerinfo supposed to be enabled for this level")19 assert.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(samppleModuleName, api.CRITICAL), "Callerinfo supposed to be enabled for this level")20 sampleCallerInfoSetting.ShowCallerInfo(samppleModuleName, api.DEBUG)21 assert.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(samppleModuleName, api.DEBUG), "Callerinfo supposed to be enabled for this level")22 sampleCallerInfoSetting.HideCallerInfo(samppleModuleName, api.DEBUG)23 assert.False(t, sampleCallerInfoSetting.IsCallerInfoEnabled(samppleModuleName, api.DEBUG), "Callerinfo supposed to be disabled for this level")24 //Reset existing caller info setting25 sampleCallerInfoSetting.showcaller = nil26 //By default caller info should be enabled for any module name27 samppleInvalidModuleName := "sample-module-name-doesnt-exists"28 assert.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(samppleInvalidModuleName, api.INFO), "Callerinfo supposed to be enabled for this level")29 assert.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(samppleInvalidModuleName, api.WARNING), "Callerinfo supposed to be enabled for this level")30 assert.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(samppleInvalidModuleName, api.ERROR), "Callerinfo supposed to be enabled for this level")31 assert.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(samppleInvalidModuleName, api.CRITICAL), "Callerinfo supposed to be enabled for this level")32 assert.True(t, sampleCallerInfoSetting.IsCallerInfoEnabled(samppleInvalidModuleName, api.DEBUG), "Callerinfo supposed to be enabled for this level")33}...
callerinfo
Using AI Code Generation
1import (2func main() {3 callerInfo()4}5func callerInfo() {6 pc, file, line, ok := runtime.Caller(1)7 fmt.Println("pc", pc)8 fmt.Println("file", file)9 fmt.Println("line", line)10 fmt.Println("ok", ok)11 fmt.Println("file", strings.Split(file, "/"))12}
callerinfo
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 fmt.Println(callerInfo())5}6func callerInfo() string {7 pc, file, line, ok := runtime.Caller(1)8 if !ok {9 }10 return fmt.Sprintf("%s:%d %s", file, line, runtime.FuncForPC(pc).Name())11}12We can use runtime.Caller() function to get the caller information. This function returns 4 values:
callerinfo
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World!")4 fmt.Println(runtime.Caller(0))5 fmt.Println(runtime.Caller(1))6 fmt.Println(runtime.Caller(2))7}
callerinfo
Using AI Code Generation
1import (2func main() {3 fmt.Println(beego.CallerInfo())4}5import (6func main() {7 fmt.Println(beego.CallerInfo())8}9[1.go:8 main()] [2.go:8 main()]
callerinfo
Using AI Code Generation
1import (2func main() {3 fmt.Println(runtime.Caller(0))4}5import (6func main() {7 fmt.Println(runtime.Caller(1))8}
callerinfo
Using AI Code Generation
1import (2func main() {3 fmt.Println("Calling function main")4 callerInfo()5 function1()6}7func function1() {8 fmt.Println("Calling function1")9 callerInfo()10 function2()11}12func function2() {13 fmt.Println("Calling function2")14 callerInfo()15 function3()16}17func function3() {18 fmt.Println("Calling function3")19 callerInfo()20}21func callerInfo() {22 pc, file, line, ok := runtime.Caller(1)23 if !ok {24 fmt.Println("No caller information")25 }26 funcName := runtime.FuncForPC(pc).Name()27 fileName := file[strings.LastIndex(file, "/")+1:]28 fmt.Printf("%s:%d %s29}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!