github.com/zhongdalu/gf@v1.0.0/g/os/glog/glog_logger_chaining.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  package glog
     8  
     9  import (
    10  	"io"
    11  
    12  	"github.com/zhongdalu/gf/g/os/gfile"
    13  )
    14  
    15  // To is a chaining function,
    16  // which redirects current logging content output to the specified <writer>.
    17  func (l *Logger) To(writer io.Writer) *Logger {
    18  	logger := (*Logger)(nil)
    19  	if l.parent == nil {
    20  		logger = l.Clone()
    21  	} else {
    22  		logger = l
    23  	}
    24  	logger.SetWriter(writer)
    25  	return logger
    26  }
    27  
    28  // Path is a chaining function,
    29  // which sets the directory path to <path> for current logging content output.
    30  func (l *Logger) Path(path string) *Logger {
    31  	logger := (*Logger)(nil)
    32  	if l.parent == nil {
    33  		logger = l.Clone()
    34  	} else {
    35  		logger = l
    36  	}
    37  	if path != "" {
    38  		logger.SetPath(path)
    39  	}
    40  	return logger
    41  }
    42  
    43  // Cat is a chaining function,
    44  // which sets the category to <category> for current logging content output.
    45  // Param <category> can be hierarchical, eg: module/user.
    46  func (l *Logger) Cat(category string) *Logger {
    47  	logger := (*Logger)(nil)
    48  	if l.parent == nil {
    49  		logger = l.Clone()
    50  	} else {
    51  		logger = l
    52  	}
    53  	if logger.path != "" {
    54  		logger.SetPath(logger.path + gfile.Separator + category)
    55  	}
    56  	return logger
    57  }
    58  
    59  // File is a chaining function,
    60  // which sets file name <pattern> for the current logging content output.
    61  func (l *Logger) File(file string) *Logger {
    62  	logger := (*Logger)(nil)
    63  	if l.parent == nil {
    64  		logger = l.Clone()
    65  	} else {
    66  		logger = l
    67  	}
    68  	logger.SetFile(file)
    69  	return logger
    70  }
    71  
    72  // Level is a chaining function,
    73  // which sets logging level for the current logging content output.
    74  func (l *Logger) Level(level int) *Logger {
    75  	logger := (*Logger)(nil)
    76  	if l.parent == nil {
    77  		logger = l.Clone()
    78  	} else {
    79  		logger = l
    80  	}
    81  	logger.SetLevel(level)
    82  	return logger
    83  }
    84  
    85  // Skip is a chaining function,
    86  // which sets stack skip for the current logging content output.
    87  // It also affects the caller file path checks when line number printing enabled.
    88  func (l *Logger) Skip(skip int) *Logger {
    89  	logger := (*Logger)(nil)
    90  	if l.parent == nil {
    91  		logger = l.Clone()
    92  	} else {
    93  		logger = l
    94  	}
    95  	logger.SetStackSkip(skip)
    96  	return logger
    97  }
    98  
    99  // Stack is a chaining function,
   100  // which sets stack options for the current logging content output .
   101  func (l *Logger) Stack(enabled bool, skip ...int) *Logger {
   102  	logger := (*Logger)(nil)
   103  	if l.parent == nil {
   104  		logger = l.Clone()
   105  	} else {
   106  		logger = l
   107  	}
   108  	logger.SetStack(enabled)
   109  	if len(skip) > 0 {
   110  		logger.SetStackSkip(skip[0])
   111  	}
   112  	return logger
   113  }
   114  
   115  // Stdout is a chaining function,
   116  // which enables/disables stdout for the current logging content output.
   117  // It's enabled in default.
   118  func (l *Logger) Stdout(enabled ...bool) *Logger {
   119  	logger := (*Logger)(nil)
   120  	if l.parent == nil {
   121  		logger = l.Clone()
   122  	} else {
   123  		logger = l
   124  	}
   125  	// stdout printing is enabled if <enabled> is not passed.
   126  	if len(enabled) > 0 && !enabled[0] {
   127  		logger.stdoutPrint = false
   128  	} else {
   129  		logger.stdoutPrint = true
   130  	}
   131  	return logger
   132  }
   133  
   134  // See Stdout.
   135  // Deprecated.
   136  func (l *Logger) StdPrint(enabled ...bool) *Logger {
   137  	return l.Stdout(enabled...)
   138  }
   139  
   140  // Header is a chaining function,
   141  // which enables/disables log header for the current logging content output.
   142  // It's enabled in default.
   143  func (l *Logger) Header(enabled ...bool) *Logger {
   144  	logger := (*Logger)(nil)
   145  	if l.parent == nil {
   146  		logger = l.Clone()
   147  	} else {
   148  		logger = l
   149  	}
   150  	// header is enabled if <enabled> is not passed.
   151  	if len(enabled) > 0 && !enabled[0] {
   152  		logger.SetHeaderPrint(false)
   153  	} else {
   154  		logger.SetHeaderPrint(true)
   155  	}
   156  	return logger
   157  }
   158  
   159  // Line is a chaining function,
   160  // which enables/disables printing its caller file path along with its line number.
   161  // The parameter <long> specified whether print the long absolute file path, eg: /a/b/c/d.go:23,
   162  // or else short one: d.go:23.
   163  func (l *Logger) Line(long ...bool) *Logger {
   164  	logger := (*Logger)(nil)
   165  	if l.parent == nil {
   166  		logger = l.Clone()
   167  	} else {
   168  		logger = l
   169  	}
   170  	if len(long) > 0 && long[0] {
   171  		logger.flags |= F_FILE_LONG
   172  	} else {
   173  		logger.flags |= F_FILE_SHORT
   174  	}
   175  	return logger
   176  }
   177  
   178  // Async is a chaining function,
   179  // which enables/disables async logging output feature.
   180  func (l *Logger) Async(enabled ...bool) *Logger {
   181  	logger := (*Logger)(nil)
   182  	if l.parent == nil {
   183  		logger = l.Clone()
   184  	} else {
   185  		logger = l
   186  	}
   187  	// async feature is enabled if <enabled> is not passed.
   188  	if len(enabled) > 0 && !enabled[0] {
   189  		logger.SetAsync(false)
   190  	} else {
   191  		logger.SetAsync(true)
   192  	}
   193  	return logger
   194  }