New form of Fermat's principle for light propagation in arbitrary (i.e. in general neither static nor stationary) gravitational field is proposed. It is based on Herglotz extension of canonical formalism and simple relation between the dynamics described by the Lagrangians homogeneous in velocities and the reduced dynamics on lower-dimensional configuration manifold. This approach is more flexible as it allows to extend immediately the Fermat principle to the case of massive particles and to eliminate any space-time coordinate, not only x0.