1- /*
2- Copyright 2017, Google, Inc.
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- */
1+ // Copyright 2017, Google, Inc.
2+ // Licensed under the Apache License, Version 2.0 (the "License");
3+ // you may not use this file except in compliance with the License.
4+ // You may obtain a copy of the License at
5+ //
6+ // http://www.apache.org/licenses/LICENSE-2.0
7+ //
8+ // Unless required by applicable law or agreed to in writing, software
9+ // distributed under the License is distributed on an "AS IS" BASIS,
10+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+ // See the License for the specific language governing permissions and
12+ // limitations under the License.
1513
1614// Command logpipe is a service that will let you pipe logs directly to Stackdriver Logging.
1715package main
1816
1917import (
2018 "bufio"
2119 "fmt"
20+ "io"
2221 "log"
2322 "os"
2423
@@ -29,51 +28,38 @@ import (
2928)
3029
3130func main () {
32- ctx := context .Background ()
33-
3431 var opts struct {
3532 ProjectID string `short:"p" long:"project" description:"Google Cloud Platform Project ID" required:"true"`
3633 LogName string `short:"l" long:"logname" description:"The name of the log to write to" default:"default"`
3734 }
38-
39- flags .Parse (& opts )
40-
41- projectID := & opts .ProjectID
42- logName := & opts .LogName
43-
44- if * projectID == "" {
45- fmt .Printf ("Please specify a project ID\n " )
46- return
35+ if _ , err := flags .Parse (& opts ); err != nil {
36+ os .Exit (2 )
4737 }
4838
4939 // Check if Standard In is coming from a pipe
5040 fi , err := os .Stdin .Stat ()
5141 if err != nil {
52- panic ( err )
42+ log . Fatalf ( "Could not stat standard input: %v" , err )
5343 }
5444 if fi .Mode ()& os .ModeNamedPipe == 0 {
55- fmt .Printf ( "Nothing is piped in so there is nothing to log!\n " )
56- return
45+ fmt .Fprintln ( os . Stderr , "Nothing is piped in so there is nothing to log!" )
46+ os . Exit ( 2 )
5747 }
5848
5949 // Creates a client.
60- client , err := logging .NewClient (ctx , * projectID )
50+ ctx := context .Background ()
51+ client , err := logging .NewClient (ctx , opts .ProjectID )
6152 if err != nil {
6253 log .Fatalf ("Failed to create client: %v" , err )
6354 }
6455
6556 // Selects the log to write to.
66- logger := client .Logger (* logName )
57+ logger := client .Logger (opts . LogName )
6758
6859 // Read from Stdin and log it to Stdout and Stackdriver
69- scanner := bufio .NewScanner (os .Stdin )
70- for scanner .Scan () {
71- text := scanner .Text ()
72- fmt .Println (text )
73- logger .Log (logging.Entry {Payload : text })
74- }
75- if err := scanner .Err (); err != nil {
76- log .Fatalf ("Failed to scan input: %v" , err )
60+ s := bufio .NewScanner (io .TeeReader (os .Stdin , os .Stdout ))
61+ for s .Scan () {
62+ logger .Log (logging.Entry {Payload : s .Text ()})
7763 }
7864
7965 // Closes the client and flushes the buffer to the Stackdriver Logging
@@ -82,5 +68,9 @@ func main() {
8268 log .Fatalf ("Failed to close client: %v" , err )
8369 }
8470
85- fmt .Printf ("Finished logging\n " )
71+ if err := s .Err (); err != nil {
72+ log .Fatalf ("Failed to scan input: %v" , err )
73+ }
74+
75+ log .Println ("Finished logging" )
8676}
0 commit comments