How to debug VCL in varnish?

28,424

Solution 1

You can see URL with requested URLs varnishlog utility (it able to write log files)

varnishlog -i RxURL

Or output some info to syslog with vmod std and syslog function for Varnish 3.x https://www.varnish-cache.org/docs/trunk/reference/vmod_std.html#syslog Varnish 5.1 https://varnish-cache.org/docs/5.1/reference/vmod_std.generated.html#func-syslog

Example:

import std;

sub vcl_recv {
  ...
  std.syslog(180, "RECV: " + req.http.host + req.url);
  ...
}

Or with C-snippet on Varnish 2.x https://www.varnish-cache.org/trac/wiki/VCLExampleSyslog

Solution 2

Using a vcl config file, import the additional included "standard library", which includes a bunch of utility functions:

import std;

# To 'varnishlog'
std.log("varnish log info:" + req.host);

# To syslog
std.syslog( LOG_USER|LOG_ALERT, "There is serious trouble");

v6.x - https://varnish-cache.org/docs/6.0/reference/vmod_generated.html#void-log-string-s

v5.x - https://varnish-cache.org/docs/5.0/reference/vmod_std.generated.html?#func-log

v4.x - https://varnish-cache.org/docs/4.0/reference/vmod_std.generated.html?#func-log

v3.x - https://varnish-cache.org/docs/3.0/reference/vmod_std.html#log

See also man varnishlog

Share:
28,424

Related videos on Youtube

lichengwu
Author by

lichengwu

coding @ taobao.com about me http://blog.lichengwu.cn

Updated on April 17, 2020

Comments

  • lichengwu
    lichengwu about 4 years

    How can I print a log in VCL?

    Can I print log info on screen?

    Can I do like this?

    sub vcl_recv {
      ....
      log.info(req.http.host); // can i write a log here?
      ....
    }
    
    • Tamil
      Tamil over 11 years
      Check if you are asking for varnishd -Cf file_name
  • lichengwu
    lichengwu almost 12 years
    VCC-compiler failed:Message from VCC-compiler: Expected an action, 'if', '{' or '}' ('input' Line 49 Pos 9) std.syslog(180, "RECV: " + req.http.host + req.url);
  • ghloogh
    ghloogh over 11 years
    Ok, yes, we forgot to import std vmod functions with: import std; varnish-cache.org/docs/3.0/reference/vmod.html
  • lichengwu
    lichengwu over 11 years
    Thanks. I can see the log in file /var/log/syslog.
  • Crowie
    Crowie almost 8 years
    Adding comment for later editing: varnish vmod_std 3.0 at varnish-cache.org/docs/3.0/reference/vmod_std.html#syslog and 4.0 at varnish-cache.org/docs/4.0/reference/…