Skip to content

Commit b75f5a8

Browse files
committed
Merge pull request #432 from CodeNow/SAN-3496-json-logs
San 3496 json logs
2 parents e4a08f7 + 4b44bef commit b75f5a8

9 files changed

Lines changed: 82 additions & 57 deletions

File tree

ansible/group_vars/all.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ loggly_username: ops
3232
## cores and logs
3333
##
3434

35-
app_log_dir: /var/log
35+
app_log_dir: /var/log/runnable
3636
core_file_dir: /var/log/core
3737

3838
##
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
- name: copy bash aliases to ubuntu
3+
tags: [ loggly, bash_aliases ]
4+
template:
5+
src=dot_bash_aliases.sh.j2
6+
dest=/home/ubuntu/.bash_aliases
7+
owner=ubuntu
8+
group=ubuntu
9+
mode=0700
10+
11+
- name: copy bash aliases to root
12+
tags: [ loggly, bash_aliases ]
13+
become: true
14+
template:
15+
src=dot_bash_aliases_root.sh.j2
16+
dest=/root/.bash_aliases
17+
owner=root
18+
group=root
19+
mode=0700
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Follows the logfile for a given app_name interpolating the datetime string into the logpath (/var/log/runnable/YYYY/MM/DD/HH/<app_name>.log)
2+
# Usage: logtail <app_name>
3+
function logtail() {
4+
local app_name="$1"
5+
local datetime=`date +%Y/%m/%d/%H`
6+
local app_log_dir="{{ app_log_dir }}"
7+
local logfile="${app_log_dir}/${datetime}/${app_name}.log"
8+
tail -f ${logfile} | bunyan
9+
}
10+
11+
# Outputs contents of an npm start log for <app_name>, if it exists, into a pager for reading.
12+
# Usage: npmlog <app_name>
13+
function npmlog() {
14+
local app_name="$1"
15+
local app_log_dir="/var/log"
16+
local logfile="${app_log_dir}/${app_name}.log"
17+
less ${logfile}
18+
}
19+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Follows the logfile for a given app_name interpolating the datetime string into the logpath (/var/log/runnable/YYYY/MM/DD/HH/<app_name>.log)
2+
# Usage: logtail <app_name>
3+
function logtail() {
4+
local app_name="$1"
5+
local datetime=`date +%Y/%m/%d/%H`
6+
local app_log_dir="{{ app_log_dir }}"
7+
local logfile="${app_log_dir}/${datetime}/${app_name}.log"
8+
tail -f ${logfile} | bunyan
9+
}
10+
11+
# Outputs contents of an npm start log for <app_name>, if it exists, into a pager for reading.
12+
# Usage: npmlog <app_name>
13+
function npmlog() {
14+
local app_name="$1"
15+
local app_log_dir="/var/log"
16+
local logfile="${app_log_dir}/${app_name}.log"
17+
less ${logfile}
18+
}
19+

ansible/roles/loggly/templates/rsyslog.conf.j2 renamed to ansible/roles/loggly/files/rsyslog.conf

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ $KLogPermitNonKernelFacility on
3535
#
3636
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
3737

38+
# Runnable JSON logging format
39+
# Creates an "output formatter" template that accepts as input JSON and prints it out without any further processing ("raw JSON").
40+
# The formatting around the %msg% string is as such: start printing at the second character "2" until the end of the line "$" using the raw JSON format type.
41+
$template RunnableJSON,"%msg:2:$:jsonr%\n"
42+
3843
# Filter duplicated messages
3944
$RepeatedMsgReduction on
4045

@@ -43,11 +48,11 @@ $RepeatedMsgReduction on
4348
#
4449
$FileOwner syslog
4550
$FileGroup adm
46-
$FileCreateMode 0640
51+
$FileCreateMode 0644
4752
$DirCreateMode 0755
4853
$Umask 0022
4954
$PrivDropToUser syslog
50-
$PrivDropToGroup syslog
55+
$PrivDropToGroup adm
5156

5257
#
5358
# Where to place spool and state files

ansible/roles/loggly/meta/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
dependencies:
3+
- { role: bash_aliases }

ansible/roles/loggly/tasks/main.yml

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,6 @@
4646
owner=syslog
4747
group=syslog
4848

49-
- name: create runnable bin directory
50-
tags: loggly
51-
become: true
52-
file:
53-
path=/opt/runnable/bin
54-
state=directory
55-
mode=0755
56-
owner=ubuntu
57-
group=ubuntu
58-
59-
- name: copy rotate util script
60-
tags: loggly
61-
become: true
62-
template:
63-
src=rotate-logs.sh.j2
64-
dest=/opt/runnable/bin/rotate-{{ name }}-logs.sh
65-
mode=0755
66-
6749
- name: copy app config
6850
tags: loggly
6951
become: true
@@ -76,8 +58,8 @@
7658
- name: copy rsyslog config
7759
tags: loggly
7860
become: true
79-
template:
80-
src=rsyslog.conf.j2
61+
copy:
62+
src=rsyslog.conf
8163
dest=/etc/rsyslog.conf
8264
owner=syslog
8365
group=syslog
@@ -94,27 +76,16 @@
9476
path=/var/spool/rsyslog/stat-{{ name }}
9577
state=absent
9678

97-
- name: check for current log file
98-
tags: [loggly, deploy]
99-
stat: path="{{ app_log_dir }}/{{ name }}-daemon.log"
100-
register: log_file
101-
102-
- name: remove old log file
103-
when: log_file.stat.exists
104-
tags: [loggly, deploy]
105-
become: true
106-
file:
107-
path="{{ app_log_dir }}/{{ name }}-daemon.log"
108-
state=absent
109-
110-
- name: touch the log file
111-
tags: [loggly, deploy]
79+
- name: ensure log path
80+
tags: loggly
11281
become: true
11382
file:
114-
path="{{ app_log_dir }}/{{ name }}-daemon.log"
115-
state=touch
83+
path="{{ app_log_dir }}"
84+
state=directory
11685
owner=syslog
11786
group=adm
87+
mode=0755
88+
recurse=yes
11889

11990
- name: restart rsyslog
12091
tags: [loggly, deploy]
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
$WorkDirectory /var/spool/rsyslog
22

3-
# start log rotation via outchannel
4-
# outchannel definition
5-
$outchannel log_rotation_{{ name }},{{ app_log_dir }}/{{ name }}-daemon.log,524288000,/opt/runnable/bin/rotate-{{ name }}-logs.sh
6-
# activate the channel and log everything to it
7-
if $msg contains '{{ name }}' and $syslogfacility-text == 'local7' then :omfile:$log_rotation_{{ name }}
8-
# end log rotation via outchannel
3+
# Rotate per hour
4+
$template RotateHourly_{{ name }},"{{ app_log_dir }}/%$YEAR%/%$MONTH%/%$DAY%/%$HOUR%/{{ name }}.log"
5+
if $msg contains '{{ name }}' and $syslogfacility-text == 'local7' then { action (type="omfile" DynaFile="RotateHourly_{{ name }}" template="RunnableJSON") }
96

10-
# {{ name }} access file:
11-
#Add a tag for {{ name }} events
7+
# Loggly: Add a tag for {{ name }} events
128
$template LogglyFormat_{{ name }},"<%pri%>%protocol-version% %timestamp:::date-rfc3339% %HOSTNAME% %app-name% %procid% %msgid% [{{ loggly_token }}@41058 tag=\"runnable\" tag=\"{{ node_env }}\"] %msg%\n"
13-
if $msg contains '{{ name }}' then @@logs-01.loggly.com:6514;LogglyFormat_{{ name }}
14-
if $msg contains '{{ name }}' then stop
9+
if $msg contains '{{ name }}' and $syslogfacility-text == 'local7' then @@logs-01.loggly.com:6514;LogglyFormat_{{ name }}
10+
if $msg contains '{{ name }}' and $syslogfacility-text == 'local7' then stop

ansible/roles/loggly/templates/rotate-logs.sh.j2

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)