github.com/mongodb/grip@v0.0.0-20240213223901-f906268d82b9/send/call_site.go (about)

     1  /*
     2  Call Site Sender
     3  
     4  Call site loggers provide a way to record the line number and file
     5  name where the logging call was made, which is particularly useful in
     6  tracing down log messages.
     7  
     8  This sender does *not* attach this data to the Message object, and the
     9  call site information is only logged when formatting the message
    10  itself. Additionally the call site includes the file name and its
    11  enclosing directory.
    12  
    13  When constructing the Sender you must specify a "depth"
    14  argument This sets the offset for the call site relative to the
    15  Sender's Send() method. Grip's default logger (e.g. the grip.Info()
    16  methods and friends) requires a depth of 2, while in *most* other
    17  cases you will want to use a depth of 1. The LogMany, and
    18  Emergency[Panic,Fatal] methods also include an extra level of
    19  indirection.
    20  
    21  Create a call site logger with one of the following constructors:
    22  
    23  	NewCallSiteConsoleLogger(<name>, <depth>, <LevelInfo>)
    24  	MakeCallSiteConsoleLogger(<depth>)
    25  	NewCallSiteFileLogger(<name>, <fileName>, <depth>, <LevelInfo>)
    26  	MakeCallSiteFileLogger(<fileName>, <depth>)
    27  */
    28  package send
    29  
    30  // NewCallSiteConsoleLogger returns a fully configured Sender
    31  // implementation that writes log messages to standard output in a
    32  // format that includes the filename and line number of the call site
    33  // of the logger.
    34  func NewCallSiteConsoleLogger(name string, depth int, l LevelInfo) (Sender, error) {
    35  	return setup(MakeCallSiteConsoleLogger(depth), name, l)
    36  }
    37  
    38  // MakeCallSiteConsoleLogger constructs an unconfigured call site
    39  // logger that writes output to standard output. You must set the name
    40  // of the logger using SetName or your Journaler's SetSender method
    41  // before using this logger.
    42  func MakeCallSiteConsoleLogger(depth int) Sender {
    43  	s := MakeNative()
    44  	_ = s.SetFormatter(MakeCallSiteFormatter(depth))
    45  
    46  	return s
    47  }
    48  
    49  // NewCallSiteFileLogger returns a fully configured Sender
    50  // implementation that writes log messages to a specified file in a
    51  // format that includes the filename and line number of the call site
    52  // of the logger.
    53  func NewCallSiteFileLogger(name, fileName string, depth int, l LevelInfo) (Sender, error) {
    54  	s, err := MakeCallSiteFileLogger(fileName, depth)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	return setup(s, name, l)
    60  }
    61  
    62  // MakeCallSiteFileLogger constructs an unconfigured call site logger
    63  // that writes output to the specified hours. You must set the name of
    64  // the logger using SetName or your Journaler's SetSender method
    65  // before using this logger.
    66  func MakeCallSiteFileLogger(fileName string, depth int) (Sender, error) {
    67  	s, err := MakeFileLogger(fileName)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	if err := s.SetFormatter(MakeCallSiteFormatter(depth)); err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	return s, nil
    77  }