ch01 + ch02
This commit is contained in:
parent
37392961c0
commit
1a37e65650
4 changed files with 96 additions and 0 deletions
BIN
.DS_Store
vendored
Normal file
BIN
.DS_Store
vendored
Normal file
Binary file not shown.
24
ch01/customLog.go
Normal file
24
ch01/customLog.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var LOGFILE = "/tmp/mGo.log"
|
||||
|
||||
func main() {
|
||||
f, err := os.OpenFile(LOGFILE, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// LstdFlags = Ldate | Ltime // initial values for the standard logger
|
||||
iLog := log.New(f, "customLog ", log.LstdFlags)
|
||||
iLog.Println("Hello there!")
|
||||
iLog.Println("Another log entry!")
|
||||
}
|
||||
27
ch01/customLogLineNumber.go
Normal file
27
ch01/customLogLineNumber.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var LOGFILE = "/tmp/mGo.log"
|
||||
|
||||
func main() {
|
||||
f, err := os.OpenFile(LOGFILE, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// LstdFlags = Ldate | Ltime // initial values for the standard logger
|
||||
iLog := log.New(f, "customLogLineNumber ", log.LstdFlags)
|
||||
|
||||
// Print the line number of when and where something has happened
|
||||
iLog.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
iLog.Println("Hello there!")
|
||||
iLog.Println("Another log entry!")
|
||||
}
|
||||
45
ch02/logDefer.go
Normal file
45
ch02/logDefer.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var LOGFILE = "/tmp/mGo.log"
|
||||
|
||||
func one(aLog *log.Logger) {
|
||||
aLog.Println("--------")
|
||||
defer aLog.Println("--------")
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
aLog.Println(i)
|
||||
}
|
||||
}
|
||||
|
||||
func two(aLog *log.Logger) {
|
||||
aLog.Println("--------")
|
||||
defer aLog.Println("--------")
|
||||
|
||||
for i := 10; i > 0; i-- {
|
||||
aLog.Println(i)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
f, err := os.OpenFile(LOGFILE, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// LstdFlags = Ldate | Ltime // initial values for the standard logger
|
||||
iLog := log.New(f, "customLog ", log.LstdFlags)
|
||||
iLog.Println("Hello there!")
|
||||
iLog.Println("Another log entry!")
|
||||
|
||||
one(iLog)
|
||||
two(iLog)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue